473,898 Members | 2,130 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(DataIn stance) 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.Pa rams("__EVENTTA RGET"). 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", "SELECTEDVA LUE" 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 4380
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.Pa rams 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.Pa rams 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 IPostBackHandle r and fire the event. However, with other controls, you can't cast them as IPostBackHandle r, 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.LoadDat a(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
4536
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 (basically have the data binding done in the If NOT IsPostBack Then statement). How come? How does this information get preserved? Which raises another question. Becuase I want to get away from the spaghetti code approach, I would like to create...
13
3688
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 first starts, but when I click on a button, and the page comes back, my dynamically created grid and controls are not visible. Any thoughts? Please view my sample code below. If Not ispostback then creategrids end if
5
2532
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. On load I loop through a count, say 0 to 3, and dynamically build rows, into the asp table.
3
2570
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 consituent control caused a postback. for example a have a consituent controls with two buttons on it "button1" and "button2" I have registered my control for postbacks using
6
2047
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 list form the calling page on postback. Can I do this? If I try and use FindControl on the placeholder, it can't find the dropdownlist. I tried...
2
2419
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 answer dialog. The user answers a question, and the next subquestion appears (using dynamic html display:none|block) depending on his answer.
2
4264
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. if i could explain: i have a list of reports in a datalist. a user clicks a report link. it goes off to reporting services, fetches the parameters for this report and displays x number of textboxes (and labels) for those parameters (hence it...
2
5041
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 is this, I have a page which uses multiple postbacks to generate a list of dynamic text boxes with appropriate labels. However, when I do the final postback to enter the values in the dynamic textboxes into the database the values seem to become...
4
4771
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 to demonstrate my issues. Lets say I have a WebForm with a DropDownList where the user selects a number from 1 to 10 (the DropDownList is not dynamically created). I also have a button on there that I use to trigger a PostBack. Based on the...
0
9993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10946
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9658
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8034
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5877
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4705
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3303
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.