PC Review


Reply
Thread Tools Rate Thread

Event Handler that creates adds another event handler

 
 
kaczmar2@gmail.com
Guest
Posts: n/a
 
      22nd Feb 2007
I have an ASP.NET page where controls are created dynamically, and I
have an issue where one event handler creates another set of controls,
and then adds event handlers to those controls. The problem comes in
where I need to raise the event in the second control - the event does
not fire. I have distilled the example below down to it simplest:

on Page_Load one button is created, and a Click event hander is added
to the button. When this first button is clicked, it adds another
button and adds another onclick event for this new button.

The first time through, everything works - The first button is
clicked, and then the second button is added with its event handler.
The problem comes on this second postback - the Page_Load event fires,
but not the first button's event - the one the creates the second
button and EventHandler.

How can I get around this? How can I add controls and event handlers
from an event hander? Code is below:

<%@ Page Language="C#" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Button button1 = new Button();
button1.ID = "button1";
button1.Text = "Button 1";
button1.Click += new EventHandler(button1_Click);
main.Controls.Add(button1); // main is the div
}

void button1_Click(object sender, EventArgs e)
{
Button button2 = new Button();
button2.ID = "button2";
button2.Text = "Button 2";
button2.Click += new EventHandler(button2_Click);
main.Controls.Add(button2); // main is the div
}

void button2_Click(object sender, EventArgs e)
{
// this event handler is never hit.
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>EventHandler Test</title>
</head>
<body>
<form id="form1" runat="server">
<div id="main" runat="server"></div>
</form>
</body>
</html>

 
Reply With Quote
 
 
 
 
Damien
Guest
Posts: n/a
 
      22nd Feb 2007
On Feb 22, 2:00 am, (E-Mail Removed) wrote:
> I have an ASP.NET page where controls are created dynamically, and I
> have an issue where one event handler creates another set of controls,
> and then adds event handlers to those controls. The problem comes in
> where I need to raise the event in the second control - the event does
> not fire. I have distilled the example below down to it simplest:
>
> on Page_Load one button is created, and a Click event hander is added
> to the button. When this first button is clicked, it adds another
> button and adds another onclick event for this new button.
>
> The first time through, everything works - The first button is
> clicked, and then the second button is added with its event handler.
> The problem comes on this second postback - the Page_Load event fires,
> but not the first button's event - the one the creates the second
> button and EventHandler.
>
> How can I get around this? How can I add controls and event handlers
> from an event hander? Code is below:
>
> <%@ Page Language="C#" %>
>
> <script runat="server">
> protected void Page_Load(object sender, EventArgs e)
> {
> Button button1 = new Button();
> button1.ID = "button1";
> button1.Text = "Button 1";
> button1.Click += new EventHandler(button1_Click);
> main.Controls.Add(button1); // main is the div
> }
>
> void button1_Click(object sender, EventArgs e)
> {
> Button button2 = new Button();
> button2.ID = "button2";
> button2.Text = "Button 2";
> button2.Click += new EventHandler(button2_Click);
> main.Controls.Add(button2); // main is the div
> }
>
> void button2_Click(object sender, EventArgs e)
> {
> // this event handler is never hit.
> }
>
> </script>
>
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
> <title>EventHandler Test</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div id="main" runat="server"></div>
> </form>
> </body>
> </html>


Hi,

As you correctly pointed out, the problem is that during the second
postback, the additional button/event handlers are not added to the
page, so no event handlers fire.

If you have a small number of states through which your page can
transition, I'd recommend enumerating those, and tracking which state
your page is in using the viewstate. Then define a routine that
creates appropriate controls based on which state you're in. Then your
button clicks become:

state = newState;
BuildPageControls();

and you also call BuildPageControls during any postback (so it'll need
to be callable multiple times during a single postback, and not end up
creating duplicate controls)

That's one approach, if you've got a small number of states. If you've
got more complex logic, you may end up having to put more into your
viewstate to allow you to safely reconstruct the page.

Finally, does Button 1 have to add the new Button 2? Could it not just
exist on the page but be disabled?

Damien

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it any point to make these two event handler into one event handler Tony Johansson Microsoft C# .NET 1 22nd Dec 2012 01:02 AM
how can I creates a bookmark and adds a hyperlink and go back to . dapanpan Microsoft Word Document Management 13 26th Jan 2009 03:59 PM
Double Click on Button creates javascript instead of taking me to the event handler on the page class Icon Iconoclast Microsoft ASP .NET 3 10th Nov 2008 11:12 AM
How to Remove an Event Handler without knowing the name of the handler Charles Law Microsoft VB .NET 4 10th Jun 2004 03:48 PM
Create new event handler or override existing handler? =?Utf-8?B?V29vbiBLaWF0?= Microsoft Dot NET Framework Forms 1 5th Jan 2004 09:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:44 AM.