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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Spectre1337
    New Member
    • Apr 2007
    • 16

    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 ...

    Code:
    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.

    Code:
    class DynamicList : UserControl
    {
    	protected void Page_Load( object sender, EventArgs e )
    	{
    		Label l;
    
    		foreach( string s in Profile.messages )
    		{
    			l = new Label();
    			l.Text = s + "<br />";
    			Controls.Add( l );
    		}
    	}
    }
    A no-brainer: This is how I'm creating the dynamic list. The entire user control works as follows:

    Code:
    class Shoutbox : UserControl
    {
    	DynamicList list = new DynamicList();
    	TextBox textbox = new TextBox();
    	Button button = new Button();
    
    	protected void Page_Load( object sender, EventArgs e )
    	{
    		button.Click += delegate
    		{
    			Profile.messages.Add( textbox.Text );
    		};
    
    		Controls.Add( list );
    		Controls.Add( textbox );
    		Controls.Add( button );
    	}
    }
    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?
  • gomzi
    Contributor
    • Mar 2007
    • 304

    #2
    Originally posted by Spectre1337
    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 ...

    Code:
    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.

    Code:
    class DynamicList : UserControl
    {
    	protected void Page_Load( object sender, EventArgs e )
    	{
    		Label l;
    
    		foreach( string s in Profile.messages )
    		{
    			l = new Label();
    			l.Text = s + "<br />";
    			Controls.Add( l );
    		}
    	}
    }
    A no-brainer: This is how I'm creating the dynamic list. The entire user control works as follows:

    Code:
    class Shoutbox : UserControl
    {
    	DynamicList list = new DynamicList();
    	TextBox textbox = new TextBox();
    	Button button = new Button();
    
    	protected void Page_Load( object sender, EventArgs e )
    	{
    		button.Click += delegate
    		{
    			Profile.messages.Add( textbox.Text );
    		};
    
    		Controls.Add( list );
    		Controls.Add( textbox );
    		Controls.Add( button );
    	}
    }
    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.

    Comment

    • Spectre1337
      New Member
      • Apr 2007
      • 16

      #3
      Originally posted by gomzi
      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 ...

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #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.

        Comment

        • Spectre1337
          New Member
          • Apr 2007
          • 16

          #5
          Originally posted by Plater
          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.

          Comment

          Working...