473,624 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET 2.0: Order of Execution Problem with Event Handlers

16 New Member
Hello,

I'm having huge difficulties solving what should be a relatively trivial problem. The following is a gross simplification (obviously it's not that simple in reality) but it will serve its purpose:

I need to program a dynamically generated list, kind of like a shoutbox, that stores the messages in a Profile variable. I know, I know, the messages would be lost as soon as the session expires, but that'll be sufficient for this simplification.

Let's assume I have a Profile variable as such ...

Expand|Select|Wrap|Line Numbers
  1. List<string> messages = new List<string>();
The user interface of my "web user control" is composed of the message list, a text box and a submit button. Each time the submit button is pressed, the message in the text box should be appended to the list.

Expand|Select|Wrap|Line Numbers
  1. class DynamicList : UserControl
  2. {
  3.     protected void Page_Load( object sender, EventArgs e )
  4.     {
  5.         Label l;
  6.  
  7.         foreach( string s in Profile.messages )
  8.         {
  9.             l = new Label();
  10.             l.Text = s + "<br />";
  11.             Controls.Add( l );
  12.         }
  13.     }
  14. }
A no-brainer: This is how I'm creating the dynamic list. The entire user control works as follows:

Expand|Select|Wrap|Line Numbers
  1. class Shoutbox : UserControl
  2. {
  3.     DynamicList list = new DynamicList();
  4.     TextBox textbox = new TextBox();
  5.     Button button = new Button();
  6.  
  7.     protected void Page_Load( object sender, EventArgs e )
  8.     {
  9.         button.Click += delegate
  10.         {
  11.             Profile.messages.Add( textbox.Text );
  12.         };
  13.  
  14.         Controls.Add( list );
  15.         Controls.Add( textbox );
  16.         Controls.Add( button );
  17.     }
  18. }
Again, very simple concept. The problem is that the list box "lags behind" one step because the event handlers are, oddly enough, called after the page is reloaded through Page_Load().

Rendering the dynamic list by overriding RenderControl is out of the question as there are several AJAX controls on that list and you can't "manually" render those.

Is there any solution to my problem?
Apr 13 '07 #1
4 1967
gomzi
304 Contributor
Hello,

I'm having huge difficulties solving what should be a relatively trivial problem. The following is a gross simplification (obviously it's not that simple in reality) but it will serve its purpose:

I need to program a dynamically generated list, kind of like a shoutbox, that stores the messages in a Profile variable. I know, I know, the messages would be lost as soon as the session expires, but that'll be sufficient for this simplification.

Let's assume I have a Profile variable as such ...

Expand|Select|Wrap|Line Numbers
  1. List<string> messages = new List<string>();
The user interface of my "web user control" is composed of the message list, a text box and a submit button. Each time the submit button is pressed, the message in the text box should be appended to the list.

Expand|Select|Wrap|Line Numbers
  1. class DynamicList : UserControl
  2. {
  3.     protected void Page_Load( object sender, EventArgs e )
  4.     {
  5.         Label l;
  6.  
  7.         foreach( string s in Profile.messages )
  8.         {
  9.             l = new Label();
  10.             l.Text = s + "<br />";
  11.             Controls.Add( l );
  12.         }
  13.     }
  14. }
A no-brainer: This is how I'm creating the dynamic list. The entire user control works as follows:

Expand|Select|Wrap|Line Numbers
  1. class Shoutbox : UserControl
  2. {
  3.     DynamicList list = new DynamicList();
  4.     TextBox textbox = new TextBox();
  5.     Button button = new Button();
  6.  
  7.     protected void Page_Load( object sender, EventArgs e )
  8.     {
  9.         button.Click += delegate
  10.         {
  11.             Profile.messages.Add( textbox.Text );
  12.         };
  13.  
  14.         Controls.Add( list );
  15.         Controls.Add( textbox );
  16.         Controls.Add( button );
  17.     }
  18. }
Again, very simple concept. The problem is that the list box "lags behind" one step because the event handlers are, oddly enough, called after the page is reloaded through Page_Load().

Rendering the dynamic list by overriding RenderControl is out of the question as there are several AJAX controls on that list and you can't "manually" render those.

Is there any solution to my problem?

To be frank, I really dont understand your code, but since you mentioned that the event handlers are called after the page is reloaded, why not check for a postback in the page load?
Its just a suggestion. aint sure that it gonna solve the prob.
Apr 13 '07 #2
Spectre1337
16 New Member
To be frank, I really dont understand your code, but since you mentioned that the event handlers are called after the page is reloaded, why not check for a postback in the page load?
Its just a suggestion. aint sure that it gonna solve the prob.
well, let me put it another way
  1. Page_Load(): A dynamic control is populated from a list. An event handler is attached to the Click event of an ASP button so that it adds an item to the list.
  2. (User Clicks on the Button)
  3. Page_Load(): Is called and the old list is rendered to the screen.
  4. OnClick(): Is called and the element is added to the list.

sigh ... or, let me put it yet another way

Initial state: List<string> list = { }; // no elements!

Page_Load(): State of list = { }
(User Clicks Button and Triggers Event)
Page_Load(): State of list = { }
OnClick(): State of list = { "message" }
// the page shows nothing
(User Clicks Button for the Second Time and Triggers Event)
Page_Load(): State of list = { "message" }
OnClick(): State of list = { "message", "message2" }
// page shows only one message when there are, in fact, two messages in the list at this point

it can't be that hard to grasp ...
Apr 16 '07 #3
Plater
7,872 Recognized Expert Expert
I see what you mean and yes I think you are right, your button handler will be executed after the page_load.
There are a number of ways you could go about this though, here's 2.

A) change your button to act like a submit button and then in your page_load() look for the params[] data corrosponding to the user's fields and add it to your list...THEN go trhough your list and create those Labels as you did before.

B) Even if it's NOT like a submit button, it is still sending a value back that can be traced (it's id name is like "__ASP_CALLBACK _BUTTON_CLICK" or something) and the code knows when it sees that to fire your onClick event. Look for that in the page_load(), call a function that takes the values from the user fields and puts them in your dynamic list, then do the label creation thing and have your button_click function do nothing.
Apr 16 '07 #4
Spectre1337
16 New Member
I see what you mean and yes I think you are right, your button handler will be executed after the page_load.
There are a number of ways you could go about this though, here's 2.

A) change your button to act like a submit button and then in your page_load() look for the params[] data corrosponding to the user's fields and add it to your list...THEN go trhough your list and create those Labels as you did before.

B) Even if it's NOT like a submit button, it is still sending a value back that can be traced (it's id name is like "__ASP_CALLBACK _BUTTON_CLICK" or something) and the code knows when it sees that to fire your onClick event. Look for that in the page_load(), call a function that takes the values from the user fields and puts them in your dynamic list, then do the label creation thing and have your button_click function do nothing.
makes perfect sense! thank you.
Apr 16 '07 #5

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

Similar topics

4
2529
by: Dave H | last post by:
I originally posted a message "Getting the current cursor position using clientX,clientY" in which I hypothesised that the problem I was having was related to event handler execution order. I am now convinced that this is the issue. In the code posted below, I am attempting to demonstrate the problem. If you load the code below into IE (the behavior is the same in Firefox, but the example code is IE specific), then click the first...
13
2572
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the original System.Web.UI.Page in order to have common checks in the page base. i make securirty checks in the page base page_load event. if the security fails, i can do whatever i want before the "real" / derived page gets to be executed.
23
4012
by: roman | last post by:
Hi, I would like to have two actions for one event. But I want the second action to trigger when the first one action completes. Is it possible to do this in javascript? I'm using the onclick event. Thanks, Roman
0
8679
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
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,...
1
6110
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
5563
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
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
2606
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
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
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.