Search code examples
c#asp.netcomposite-controls

CreateChildControls called twice


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?


Solution

  • 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.