Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Events, Viewstate, OnPreRender it has to be a bug

Reply
Thread Tools

Events, Viewstate, OnPreRender it has to be a bug

 
 
Kenton Smeltzer
Guest
Posts: n/a
 
      10-22-2003
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

 
Reply With Quote
 
 
 
 
Jalil Vaidya
Guest
Posts: n/a
 
      10-23-2003
> 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

 
Reply With Quote
 
 
 
 
Kenton Smeltzer
Guest
Posts: n/a
 
      10-23-2003
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> > 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
>



 
Reply With Quote
 
Kenton Smeltzer
Guest
Posts: n/a
 
      10-23-2003
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OnPreRender RN1 ASP .Net 0 03-07-2008 09:57 PM
Understanding OnPreRender and how to use it Tarun Mistry ASP .Net 3 02-13-2006 07:33 PM
OnPreRender ??? What is ? Daniel Groh ASP .Net 4 06-10-2005 12:29 PM
How can i handle post back event of the control that is added on the OnPreRender () method Umut Tezduyar ASP .Net 1 10-06-2004 09:10 AM
Problem with adding UserControls programtic in the OnPreRender event and still keep the UC viewState Flare ASP .Net 5 11-21-2003 06:56 PM



Advertisments