472,345 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

Events, Viewstate, OnPreRender it has to be a bug

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

Nov 17 '05 #1
3 2328
> 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

Nov 17 '05 #2
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

Nov 17 '05 #3
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
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kenton Smeltzer | last post by:
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...
5
by: Flare | last post by:
(ASP.NET 1.1) Hi I have a problem with my ViewStates in a userControl. The problem is that the ViewState is not "writte" og changed if you like...
2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
3
by: Harry | last post by:
Dear all, It is found that when a webform control trigger an event, Private Sub Page_Load(ByVal sender As System.Object, ByVal e As...
3
by: Francois | last post by:
Hi, First of all sorry for the long post but I believe it is quite an interesting as well as advanced and complex problem. I have a problem...
0
by: tshad | last post by:
I am trying to figure out how to handle a couple of problem using multiple events tied to the same button. Following is the button: ...
2
by: Oddball | last post by:
I am trying to create and add a number of ImageButtons to a page when a certain event is called. Not only am I suffering from my page running the...
4
by: Rolf Welskes | last post by:
Hello, the problem seems simple I have an own webcontrol one property is public bool IsEdit { get
2
by: Gary W. Smith | last post by:
I have a page that inherits from a base page that is currently overriding all of the On* events. For the most part I'm accomplishing everything I...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.