473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 2405
> 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 then maybe someone can see something I missed. ...
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 after editing in my case a textbox. This is...
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 System.EventArgs) Handles MyBase.Load End Sub This...
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 with the checkbox control I have in my repeater. I...
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: <asp:button ID="submitAnswer" CommandName="SubmitAnswer"...
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 "Page_Load" event three times, but I can't seem...
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 set out to do with the inheritance, but I wanted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.