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;
}
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.