Re: Dynamic Button with Event
- From: "Scott M." <s-mar@xxxxxxxxxxxxx>
- Date: Tue, 24 Nov 2009 14:47:36 -0500
"Mr Not So Know It All" <djscratchnsniffing@xxxxxxxxx> wrote in message
news:cb5439ef-4b4e-4045-8e4b-d994cfcd6d16@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I've new to .NET. I'm trying to add a button dynamically with an
event. Initially, I have a button with a click. The original button's
click event response writes a test message and adds a button. The new
button has a click event attached with a test message. Unfortunately,
when I click the dynamically attached button, nothing happens. Could
someone walk me throught this?
[aspNET:CODE]
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="plh" runat="server">
<asp:Button ID="btn_Click" Text="Click" runat="server" />
</asp:PlaceHolder>
</div>
</form>
</body>
</html>
[U]CODEFILE[/U]
public partial class dynamicEvent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btn_Click.Click +=new EventHandler(Clicked);
}
public void Clicked(object sender, EventArgs e)
{
Response.Write("Button One Clicked");
Button btn = new Button();
plh.Controls.Add(btn);
btn.Text = "New Click";
btn.Click += new EventHandler(btn_Click2);
}
void btn_Click2(object sender, EventArgs e)
{
Response.Write("Dynamic Button Clicked");
}
}
[/aspNET:CODE]
Have you tried making sure that the LAST thing you do to the newly created
button, is add it to the Controls collection, rather than adding it and then
setting up the event handler?
-Scott
.
- Follow-Ups:
- Re: Dynamic Button with Event
- From: Mr Not So Know It All
- Re: Dynamic Button with Event
- References:
- Dynamic Button with Event
- From: Mr Not So Know It All
- Dynamic Button with Event
- Prev by Date: Re: Should my domain name start with www?
- Next by Date: Re: Dynamic Button with Event
- Previous by thread: Dynamic Button with Event
- Next by thread: Re: Dynamic Button with Event
- Index(es):
Relevant Pages
|