R – Asp.Net CreateChildControls() – Re create controls with Postback data – Event


I have a WebPart custom control (Composite) in .Net, which gets created on page load to show a Chart using 'Dundas Charting Controls' (this is created by a user control inside the page). I get the properties for this control from the database.

I have another control, which is a Filter (outside webpart) and based on data of this filter control which the user selects and which I would get on postback after click of button, I have to show the filtered chart results. The problem is CreateChildControls() gets called before the postback data is available (which would be available only after the Page_Load event fires).

I'm unable to get this data in time to pass on the parameters for filtering the Chart Results.

The implementation os like this …


Page > User Control > Webparts > Composite Control/Chart


Page > User Control > Composite Control [I get this data on Postback]

Best Solution

It sounds like you are running into an event ordering issue. I always try to make my controles relatively dump - so they don't really know how they are being used.

Consider creating a method in your chart control to force an update of its data:

public void UpdateChart(-- arguments as needed --)

then create an event in your composit control (that has your filters) like

public event Eventhandler FiltersChanged;

Assign this to an event hander on parent page:

filterControl.FiltersChanged += new EventHandler(Filter_OnChange)

Then create an event handler that tells your chart control about the change

Filter_OnChange(Object sender, EventArgs e)
   // get whatever data you need from your filter control
   // tell the chart about the new data and have it reload/redraw
   myChart.UpdateData( - filter options here -}

In doing so, you let the page direct the order of operations and do not rely on the order in which the child controls Load methods are called.

Related Question