Events, Viewstate, OnPreRender it has to be a bug

  • Thread starter Thread starter Kenton Smeltzer
  • Start date Start date
K

Kenton Smeltzer

Hello All,

I am having a problem with events and the addition of
controls on a page I am developing. First let me tell you
what I have tried and then maybe someone can see something
I missed.

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.

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
as expected and the events fired before the PreRender the
only problem is that any new control added at PreRender
does not maintain its state. Textboxes loose there text
and any handlers attached to the control loose reference
to the delegate they where pointing to. Anyway I need to
find some way to add controls to the control collection
dynamically and get them down to the browser. Right now I
have a real nasty method that just clears all child
controls and calls the Page_Load again and then when
Page_Load creates the control it calls a delegate to give
the control that launched it a handle to it. But there has
got to be a better way.

How can I get these controls added before the page is
render and get them to keep their state information.

Thanks,
Kenton
 
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
 
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.
 
Never mind I fixed it. One of the controls ID was getting changed on the
second postback. I found it and fixed it.

Thanks,
Kenton
 
Back
Top