473,398 Members | 2,335 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,398 software developers and data experts.

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

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 1959
gomzi
304 100+
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
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 Expert 4TB
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
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
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...
13
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...
23
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...

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.