0

I created ASP.NET Chart dynamically. Now I have to set OnClick event to Chart object programmatically.

This is what i tried so far:

double[] yValues = { 71.15, 23.19, 5.66 };
string[] xValues = { "AAA", "BBB", "CCC" };
Chart temp1 = new Chart();
temp1.ID = "ChartArea1";
temp1.Series.Add(new Series("Series1"));
temp1.ChartAreas.Add(new ChartArea("ChartArea1"));
temp1.Series["Series1"].Points.DataBindXY(xValues, yValues);
temp1.Series["Series1"].PostBackValue = "#VALY-#VALX";
temp1.Click += new ImageMapEventHandler(Chart1_Click);
temp1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;     
PlaceHolder1.Controls.Add(temp1);

protected void Chart1_Click(object sender, ImageMapEventArgs e)
{
  string test = e.PostBackValue;
}

The Chart is clickable also postback well but not firing Chart1_Click.

How to set ASP.NET Chart OnClick event programmatically?

EDIT: This is my .aspx.cs file.

My goal:

I need to create dynamic chart after I click a button and fire event when i click a column on the chart.

Thus I can't generate dynamic chart in Page_Load/Page_Init, and I also try the suggestions in How can I create a dynamic button click event on a dynamic button? but not work for me.

My question is what I have to achieve is possible? Can someone please give me some advises?

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

    }
}
protected void btnQRY_Click(object sender, EventArgs e)
{
    charTest();
}
protected void charTest()
{
    double[] yValues = { 71.15, 23.19, 5.66 };
    string[] xValues = { "AAA", "BBB", "CCC" };
    string[] xtValues = { "AAAa", "BBBa", "CCCa" };
    Chart temp1 = new Chart();
    Chart temp2 = new Chart();
    temp1.ID = "ChartArea1";
    temp1.Series.Add(new Series("Series1"));
    temp1.ChartAreas.Add(new ChartArea("ChartArea1"));
    temp1.Series["Series1"].Points.DataBindXY(xValues, yValues);

    //temp1.Load += new EventHandler(Chart1_Click);
    temp1.Series["Series1"].PostBackValue = "#VALY-#VALX";
    temp1.Click += new ImageMapEventHandler(Chart1_Click);

    PlaceHolder1.Controls.Add(temp1);

}


protected void Chart1_Click(object sender, ImageMapEventArgs e)
{
    string test = e.PostBackValue;
}
劉鎮瑲
  • 517
  • 9
  • 20

2 Answers2

0

It's a bit difficult debugging your code as the aspx page code is not posted. The first thing which stands out to me is the missing DataBind to the chart object in the click event. At least that's how I did it and it works. Might do it for you too.

KH S
  • 444
  • 1
  • 4
  • 8
  • What do you mean DataBind to the chart object in the click event? Can you show me with code? – 劉鎮瑲 Aug 29 '19 at 08:53
  • In your charTest() you do the DataBind. Maybe just calling charTest() again will probably work. It wouldn't change the values. If this is intended you need to create some code to do that. It would help if you would post the aspx page code. – KH S Aug 29 '19 at 09:24
  • I want fire event when i click dynamically created chart , but keep same data in the chart. Any idea? – 劉鎮瑲 Aug 30 '19 at 07:29
  • Hi 劉鎮瑲, I used your code for testing.The one you posted first. I put the chart creation into the page_load event and added the Chart1_Click event. I do get the postback when I click on one of the blue bars. To see the values of the bar I added a label to the aspx page above the Placeholder1 with the ID="TestPostback". I changed the code in your Chart1_Click event to: TestPostback.Text = e.PostBackValue; You then should see the value of the bar on your web page. If you click outside a blue bar nothing happens. Do you get those results too? What do you actually want to do as the final result? – KH S Aug 30 '19 at 16:34
0

You should add the dynamic controls in the Page's Init event handler so that the ViewState and Events are triggered appropriately.

 protected void Page_Init(object sender, EventArgs e)
{
    // Use Page_Init event Instead of Page_Load event 
   // in case of adding dynamic controls to trigger events properly
    charTest();
}

protected void Page_Load(object sender, EventArgs e)
{
}
protected void charTest()
{
    double[] yValues = { 71.15, 23.19, 5.66 };
    string[] xValues = { "AAA", "BBB", "CCC" };
    string[] xtValues = { "AAAa", "BBBa", "CCCa" };
    Chart temp1 = new Chart();
    Chart temp2 = new Chart();
    temp1.ID = "ChartArea1";
    temp1.Series.Add(new Series("Series1"));
    temp1.ChartAreas.Add(new ChartArea("ChartArea1"));
    temp1.Series["Series1"].Points.DataBindXY(xValues, yValues);

    //temp1.Load += new EventHandler(Chart1_Click);
    temp1.Series["Series1"].PostBackValue = "#VALY-#VALX";
    temp1.Click += new ImageMapEventHandler(Chart1_Click);

    PlaceHolder1.Controls.Add(temp1);

}


protected void Chart1_Click(object sender, ImageMapEventArgs e)
{
    string test = e.PostBackValue;
}
Nareen Babu
  • 443
  • 1
  • 5
  • 13
  • Thank your advise, but I will call charTest() when I choose some condition and click button to generate chart depending on user choices. So, I think add the chart in the Page's Init not work for me. – 劉鎮瑲 Aug 30 '19 at 07:21
  • 1
    Then in that case my suggestion is to add all controls in design phase . Make the visibility to show / hide in the runtime. – Nareen Babu Aug 30 '19 at 09:30