Jalil,
Thanks for the reply, I am pretty familiar with the lifecycle of a control
but my problem is this: I have controls down stream from the parent page
that will launch new controls from there click event. They do so by calling
a function in the parent page called launchcontrol which takes the new
control passed and adds it to the control placeholder as well as puts info
in a session object to recreate the control in Page_Load on post back. The
control has the same ID on post back yet the control that the child control
has a reference to and the control on the page are two different controls
and the control that it added on page _load does not get the viewstate for
the control that was launched via the child controls click. I can fix this
by doing the following: when the child control calls the page's
launchcontrol method I return back the object that I store in session, which
I call process. From here the child control has a handle to the process in
the session object. Then the child control hooks into a delegate in the
process object called controllaunched from there I have a handler in the
child control for the launch of the new control by page load where I can
then set the child controls reference to the new control. To do this every
time a child launches a control I have to force the Page to call Page_Load
again to get the new control to display. This is nasty, stupid, and prone to
bugs. There has to be a way to get a reference to that control without
forcing it to go through the Page_Load cycle again to make them the same
object. And to get a control added via page_Load to look for the viewstate
of the control added the time before by the controls click event.
The way I was trying to do it before was to queue all request for controls
to be created by the child controls and then build the controls in
OnPreRender but I have abandoned that idea.
"Jalil Vaidya" <ja****@mailinator.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
First I tried adding the controls and the event handlers
> for my control in the Initialize Components method of my
> page in the hopes that it would fire the event before the
> Page_Load method was called but to no avail. It seems that
> ASP.NET will not let you handle events before the page is
> loaded which is expected but it was worth a shot.
Any event on any control fires only after Page_Load. This is how the
page life cycle works.
> Second I decided to go ahead and add my controls and the
> event handlers in the Page_Load method and then add the
> new controls as the event fired and then push everything
> into the control collection at the OnPreRender this worked
Adding controls in OnPreRender is not really a good idea.
> Anyway I need to find some way to add controls to the control
>collection dynamically and get them down to the browser.
Any dynamically added control should be added to the page's control
collection every time the page is loaded and the events should be
re-subscribed to. (The data binding can be left out in postback). There
are two places you can do this. One in Page_Load as you are doing. The
other place is overriding GetControlCollection method.
Every time a page is requested(whether its a postback or not), the
control hierarchy is rebuilt by ASP.NET. The controls in the page need
to be recreated. Any controls created declaratively are added
automatically but the controls added in code behind need to be added
again. The viewstate data will be present which helps the individual
controls to determine if the data changed and whether they need to fire
an event. But if the dynamic controls are not added again then there
will be nothing present that will raise an event; presence of viewstate
data doesn't matter in this case.
HTH,
Jalil Vaidya