473,507 Members | 5,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Web Application, Postback and Dynamic User Controls

balabaster
797 Recognized Expert Contributor
I've got a user control that builds a table of dynamic data based on a :LINQ class holding the data. The data is loaded using the LoadData(DataInstance) method. The table it builds contains a number of dynamic controls that themselves have postback/autopostback so the display of the control needs to be built at latest in the Page.Load event or the event handlers for the controls don't get wired up.

Now if I have a page that uses this user control by passing the data in the page load, the control loads the data just fine. However, if I want to load the control with data as the result of a user made selection - say, a dropdownlist that has autopostback, it doesn't.

I understand why this is; on the postback, the page load event fires, which loads the control that doesn't yet contain any data because at this stage of the lifecycle the selection event handler hasn't yet been filed. Then the event handler fires which passes the data to the user control.

Now, one of two things happens depending on how I wire the control up. Either I have it reload the display, at which point, the event handlers are missing because they're required to be wired up in the page load, but I do have the data and the buttons displayed; OR, I don't reload control displaying the data at which point none of my controls have yet been loaded in the user control and consequently only the basic user control template is displayed.

So the question boils down to - what do I do about this? I found a couple of hack workarounds - i.e. reference the Page.Request.Params("__EVENTTARGET"). The only problem with this is that my user control loads a substantial amount of controls and having to parse the Request.Form object to figure out what was changed, and whether it's value field is called "VALUE", "TEXT", "SELECTEDVALUE" or some other named field... is a little laborious.

Any suggestions as to how to deal with this limitation of the page lifecycle? I'm wondering if there's a way that I can force the event handlers to be wired up in the Page PreRender event - even if I do this manually? If I can do this, then I can move the place the user control is built and still have the event handlers work - which is the way this *should* work in my opinion - but that's just me. If anyone's got any ideas, I'd welcome them...
Nov 2 '08 #1
3 4346
kenobewan
4,871 Recognized Expert Specialist
Is this a problem that can be resolved by using the (not/!) page.ispostback property? An alternative is partial page refreshes using a panel.

Or do I need to fire my speed reading teacher ;)?
Nov 2 '08 #2
balabaster
797 Recognized Expert Contributor
Haha, um, I don't think you need to fire your speed reading teacher.

However, the issue cannot be resolved by using is or is not postback. The reason for this is that the request for the ASCX to load the data doesn't occur until the postback - because we don't know what data to load until the end user tells us what they're looking for. However, all the controls' event handlers are (at the very latest) wired up in the Page.Load event, at which point we don't know what data needs loading - so we're in a Catch 22 situation.

By the time the ASCX knows which data to load, it's too late to wire up the dynamic event handlers. So we either need a way to know what data to load earlier, or find an ability to wire up the event handlers later in the page lifecycle.

To find out which data to load earlier, I can use some hack mechanism like Page.Request.Params and finding the selection control's selected index, or more preferably I'd like to add the ability for the Postback or PreRender phases to hook up the event handlers the way that PageLoad does.

Both have their advantages and drawbacks. On the plus side, Page.Request.Params at first glance is a simple mechanism that can be used for this. However, depending on the control that caused the postback, the value property may be any of TEXT, VALUE, SELECTEDVALUE, CHECKED etc etc. With a button it's nice and easy, you can cast the button as IPostBackHandler and fire the event. However, with other controls, you can't cast them as IPostBackHandler, and apparently there's no simple way of figuring out their event handler and firing them. So while this solution may be a possibility it is far from elegant as I have to Select Case/Switch the control to see what it's class type is and then call a hardwired event handler for it - which is about the suckiest solution I can think of.

Being able to wire a dynamic event handler in the PreRender - or even the PostBack phases would be far more elegant as I can wire whatever event handler I like and it will still be handled on postback. This means that I can have my ASCX build the display right after it's loaded the data and the event handlers would still be able to be handled. However, so far, I've not been able to find a method to do this. I've not been able to find a single useful explanation of how .NET actually wires up event handlers in ASP.NET in the underlying code.

I am currently using an update panel in the ASCX itself to modify the data it loads which itself works like a charm, but I can only get it to load the data in my test harness pages where I hardcode the key of the dataset I wish to load - which is used in the page load. Even if I put a button outside my update panel and put the Control.LoadData(DataInstance) call in the button postback it stops working once more, because even in the UpdatePanel, it still doesn't load the data until the PostBack (even if it's done behind the scenes asynchronously, it does a full page lifecycle) phase. It then upon getting the response just updates the content of that panel with the matching panel from the response.

So there you have it - I've explored all the options I can think of and appear to have come up against dead ends (so far) in all cases.
Nov 2 '08 #3
kenobewan
4,871 Recognized Expert Specialist
Here is an article that answers most of your questions:
Dynamic Web Controls, Postbacks, and View State

This MSDN section should also be of use:
Server Event Handling in ASP.NET Web Pages
Nov 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
4478
by: Krista Lemieux | last post by:
I'm new to ASP.NET and I'm not use to the way things are handled with this technology. I've been told that when I have a control, I should only bind the data to it once, and not on each post back...
13
3647
by: Chris Thunell | last post by:
I have created several grids dynamically and have added them to different HTML placeholders on a vb.net web form. The grids and controls within them come up and view beautifully when the web page...
5
2511
by: PCH | last post by:
I have an c# asp.net (.net 1.1) web page, viewstate on. The problem I am having is on the button click postback to update. Heres the situation: I have an asp table that has 1 header row. ...
3
2541
by: Martin | last post by:
Hi, I have created a composite control that has a number of standard asp.net controls on it that can themselves cause postbacks. What i need to do in my composite control is to determine which...
6
2033
by: Alan Silver | last post by:
Hello, I have a placeholder, which gets a user control added in when the page first loads. This user control contains a dropdownlist, and I would like to get hold of the value of this drop down...
2
2399
by: brad | last post by:
Group, I'm using Visual Studio 2003 to create an ASP.NET 1.1 project which contains nested server user controls in order to create a tree-like hierarchy. The tree is a sort of question and...
2
4233
by: =?Utf-8?B?TG91aXNhOTk=?= | last post by:
Hello. ive just read some posts on the age old issue of losing dynamic control values on postback. Most people say recreate the controls on the onInit, but i just dont think this serves my purpose....
2
5004
by: englishman69 | last post by:
Hello, I have been banging my head against this one for a while... Searches online have revealed many different proposals for correcting my issue but none that I can follow! My basic situation...
4
4740
by: =?Utf-8?B?RHlsYW5TbWl0aA==?= | last post by:
I have a WebForm where I'm dynamically creating some controls and I'm having difficulty understanding how the state is being persisted and how to work with it. I've created a simplified example...
0
7223
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
7372
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
7482
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
5623
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,...
1
5041
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...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.