NOT ANSWERED BEFORE Wiring button events in code.
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngescalate' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

David Brophy
"I tried to ask this on [aspngfreeforall] and got no answer (posted 24/7 =
&
25/7). Now I need to get this answered because the other list is not =
providing
me with valid answers. (no replies were posted at all)."

Look at the code below:

(You can see the code in action at =
http://ip1.cambro.net/v0_2/webform3.aspx )

I am trying to wire up the click event of button2, but only do it when =
button1
has been clicked. When the page is run, you click on button1. button2's =
text
changes to "Event is now wired!" as in the code, but clicking button 2 =
doesn't
then change the label to show "hello!".

If you move the "button2.Click+=3Dnew System.EventHandler(click2);" =
statement into
the "Page_Load" method then it works.

I would like to be able to pragmatically alter the behavior of the =
button. I
don't quite understand why it should not work... - Is there a way around =
this?

In my code-behind class:

protected System.Web.UI.WebControls.Label label1;
protected System.Web.UI.WebControls.Button button1;
protected System.Web.UI.WebControls.Button button2;

protected void wireEvent(object o, EventArgs e)
{
    button2.Click+=3Dnew System.EventHandler(click2);
    button2.Text=3D"Event is now wired!";
}

protected void click2(object o, EventArgs e)
{
    label1.Text=3D"hello!";
}

In my aspx page:

<html>
<body>
<form id=3D"WebForm1" method=3D"post" runat=3D"server">

<asp:Button ID=3D"button1" Runat=3Dserver=20
Text=3D"Click this to wire event for button below."=20
OnClick=3D"wireEvent"></asp:Button><br>

<asp:Button ID=3D"button2" Runat=3Dserver=20
Text=3D"Initialy no click event"></asp:Button><br>

<asp:label Runat=3Dserver ID=3Dlabel1></asp:label>

</form>
</body>
</html>

- David Brophy
Director, Cambro Limited,
(023) 80 679550=20
Click here to reveal e-mail address

Reply to this message...
 
    
Mitch Denny (VIP)
David,

Sorry for the delay on getting back to you on this, I gave this
some serious thought before I came to a sudden realisation why
it isn't going to work and why ASP.NET is behaving as it should.

Remember that a Page is stateless, even though ASP.NET tries
very hard to give the impression that it is providing real Windows
like interface. What this means is that every time you request
a page a new object is constructed, so all the event handlers
that you define in conditional (after post-back) code don't
get recreated, and that is why it doesn't get executed.

Here is the process:

    1. User-Agent requests page.
    2. ASP.NET Constructs page and renders controls.
    3. Rendered page is sent to User-Agent.
    4. Page is destroyed by ASP.NET.
    5. User-Agent posts back after user presses Button1.
    6. ASP.NET Constructs page and renders controls.
    7. ASP.NET runs event handler for postback on Button1.
        * Subscribes event handler to Button2.
    8. Rendered page is sent to User-Agent.
    9. Page is destroyed by ASP.NET.
        * Assignment of event handling delegate lost.

So you can see what is happening here right? To get around
the problem, I would subscribe all the event handlers at
construction time and have a check placed in the event
handler for Button2 that ensures that Button1 was clicked.

You need to remember that not everything is serialized and
stored; put simply it would kill performance and cause real
problems for web-farming. Its pretty much up to the control
itself what it puts into the statebag, and I guess that a
delegate (if it can be put in a statebag) wasn't part of
the design.

If they could get around the scalability issues it would
be a cool feature though!

----------------------------------------
- Mitch Denny
- http://www.warbyte.com
- Click here to reveal e-mail address
- +61 (414) 610-141
-

[Original message clipped]

Reply to this message...
 
 
System.EventArgs
System.EventHandler
System.Web.UI.WebControls.Button
System.Web.UI.WebControls.Label




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com