K
kaczmar2
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>
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>