Reputation: 10561
I have a composite server control, where I implement CreateChildControls. This control is a grid, which also includes paging and sorting controls.
When a sort link is clicked, CreateChildControls is called first, then the event handler and from the event handler I call CreateChildControls again, in order to rebuild the controls with the new sort order.
Is there any way to skip CreateChildControls when the postback was triggered by a control event handler?
Is this even possible or do I have to go through CreateChildControls before the event handler, so asp.net can hook up the event to an existing control?
Upvotes: 1
Views: 1919
Reputation: 1576
I have a similar situation, but I have two things going on: I am loading controls, and then I am displaying information based on previous selections by the user. When a user changes a dropdownlist, or clicks a button, the page is reloaded and their choices are added to the querystring. So, my solution is this:
protected override void CreateChildControls()
{
base.CreateChildControls();
AddControls();
if (Page.IsPostBack)
return;
ShowProcedure() // The time-intensive code that I don't want to call twice.
}
AddControls populates the controls, and enables the postback option. ShowProcedure uses the values from the querystring to show a lot of data. This way, I am not calling ShowProcedure on the CURRENT values.
Upvotes: 0
Reputation: 10561
I didn't know it at the time I wrote this question, but this is actually a part of the natural life cycle of a custom control.
CreateChildControls()
is called each time EnsureChildControls()
is called. The trick is to set the ChildControlsCreated
property at the end of the first call to CreateChildControls()
, so the entire process does not happen more than once.
Upvotes: 3