I have multiple day,week, and month charts on a index.aspx page. All the charts are working, and the code behind for the charts is on the index.aspx.cs, but now I need to convert all those charts to user controls (day, and week, and month charts). The main difference between these charts (ie day, week charts) is the ID and DataSourceID. I've tried to set the ID on the ascx dynamically in different ways: ie: \[code\]ID="<% Chart.ID = GetIDforControl(ID); %>"ID="<% Chart.ID %>"ID="<% GetIDforControl(ID) %>"\[/code\]etc...and then set the IDs in the ascx.cs page as show below.However, if I try to set the ID for the chart in the .ascx file, other than: ID="AWeek"I get the error below:"The ID property of a control can only be set using the ID attribute in the tag and a simple value. " Example: If I set it manually all week charts end up having same data of AWeek.or sometimes I get Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Line 100: if (chart.ID.StartsWith("AWeek"))or sometimes I get this error, if I don't hardcode the ID in the WeekChart.ascx file.Compiler Error Message: CS0103: The name 'AWeek' does not exist in the current contextI have created user controls for other parts of the site, but converting these charts to user controls has not been as easy as the other ones. I've done research online but have not found a solution.Any help would be appreciated. Thanks. I'm planning to use similar solution for the other 2 user controlsayChart.ascx
.....DayChart.ascx.cs.....MonthChart.ascx ....MonthChart.ascx.cs....index.aspx\[code\]<%@ Register src="http://stackoverflow.com/questions/14388315/UserControls/WeekChart.ascx" tagname="WeekChartIndex" tagprefix="wc" %><wc:WeekChart ID="AWeek" runat="server" /><wc:WeekChart ID="BWeek" runat="server" /><wc:WeekChart ID="CWeek" runat="server" />\[/code\]WeekChart.ascx\[code\]<asp:Chart runat="server" SkinID="WeekChart" Width="500px" ID="chartAWeek" DataSourceID="dschartAWeek" OnClick="WeekChart_Click" OnDataBound="Chart_DataBound" OnCustomize="WeekChart_Customize"> <Series> <asp:Series ChartArea="ChartArea1" Name="A" ...........> </asp:Series> <asp:Series ChartArea="ChartArea1" Name="B" ...........> </asp:Series> </Series></asp:Chart>\[/code\]WeekChart.ascx.cs\[code\]protected void Page_PreInit(object sender, EventArgs e){ UserControls_WeekChart ctrl = (UserControls_WeekChart)sender; //ctrl.ID.StartsWith Chart chart = sender as Chart; //if (chart == null) return; //if (ctrl == null) return; if (ctrl.ID.StartsWith("AWeek")) { //chart.ID = "AWeek"; chart.ID = ctrl.ClientID; chart.DataSourceID = "dschartAWeek"; //chart.DataBind(); DataBind(chart.ID.StartsWith("AWeek")); } else if (ctrl.ID.StartsWith("BWeek")) { chart.ID = "BWeek"; chart.DataSourceID = "dschartBWeek"; DataBind(chart.ID.StartsWith("BWeek")); }}protected void Page_Load(object sender, EventArgs e){ //This code was already on the index.aspx page AWeek.DataBind(); ADay.DataBind(); BWeek.DataBind(); BDay.DataBind(); CWeek.DataBind(); CDay.DataBind(); AMonth.DataBind(); BMonth.DataBind();} \[/code\]
.....DayChart.ascx.cs.....MonthChart.ascx ....MonthChart.ascx.cs....index.aspx\[code\]<%@ Register src="http://stackoverflow.com/questions/14388315/UserControls/WeekChart.ascx" tagname="WeekChartIndex" tagprefix="wc" %><wc:WeekChart ID="AWeek" runat="server" /><wc:WeekChart ID="BWeek" runat="server" /><wc:WeekChart ID="CWeek" runat="server" />\[/code\]WeekChart.ascx\[code\]<asp:Chart runat="server" SkinID="WeekChart" Width="500px" ID="chartAWeek" DataSourceID="dschartAWeek" OnClick="WeekChart_Click" OnDataBound="Chart_DataBound" OnCustomize="WeekChart_Customize"> <Series> <asp:Series ChartArea="ChartArea1" Name="A" ...........> </asp:Series> <asp:Series ChartArea="ChartArea1" Name="B" ...........> </asp:Series> </Series></asp:Chart>\[/code\]WeekChart.ascx.cs\[code\]protected void Page_PreInit(object sender, EventArgs e){ UserControls_WeekChart ctrl = (UserControls_WeekChart)sender; //ctrl.ID.StartsWith Chart chart = sender as Chart; //if (chart == null) return; //if (ctrl == null) return; if (ctrl.ID.StartsWith("AWeek")) { //chart.ID = "AWeek"; chart.ID = ctrl.ClientID; chart.DataSourceID = "dschartAWeek"; //chart.DataBind(); DataBind(chart.ID.StartsWith("AWeek")); } else if (ctrl.ID.StartsWith("BWeek")) { chart.ID = "BWeek"; chart.DataSourceID = "dschartBWeek"; DataBind(chart.ID.StartsWith("BWeek")); }}protected void Page_Load(object sender, EventArgs e){ //This code was already on the index.aspx page AWeek.DataBind(); ADay.DataBind(); BWeek.DataBind(); BDay.DataBind(); CWeek.DataBind(); CDay.DataBind(); AMonth.DataBind(); BMonth.DataBind();} \[/code\]