dynamically created button click event not firing
I have done a bit of research on this but none of the solutions I have found seem to provide a fix for my issue. I have an asp.net web app in C# and I want to dynamically add a submit button after a selection is made from a drop down list.
protected void Page_Load(object sender, EventArgs e)
{
submitButton.Text = "Submit";
submitButton.ID = "submitButton";
submitButton.Click += new EventHandler(submitButton_Click);
SelectionDropDownList.SelectedIndexChanged += new EventHandler(SelectionDropDownList_SelectedIndexChanged);
}
protected void SelectionDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
switch (SelectionDropDownList.Sel开发者_StackOverflow社区ectedIndex)
{
case 1:
//does a redirect
break;
case 2:
Panel1.Controls.Add(submitButton);
break;
case 3:
//does a redirect
break;
}
}
protected void submitButton_Click(object sender, EventArgs e)
{
//can't get this event to fire.
SubmitSearch();
}
This is a timing issue. Your program flow is like this:
- Page_Load is executed, page is rendered.
- Dropdown is selected. Post back is send to the server. Page_Load is executed. Then the event is send to your dropdown instance. SelectedIndexChanged is executed. Your button is created and the page is rendered and send to the client.
- Button is pushed. Post back is send to the server. Page_Load is executed. Asp tries to execute the event, but the button does not exist anymore. So the event is ignored.
That's one of the nasty details of Webforms and a good reason not to use it - if you are free to choose. If you have to use it, http://msdn.microsoft.com/en-us/library/ms178472.aspx might be of help.
How about instead of dynamically adding the control, always add it, but set Visible=false
initially. Then where you're currently adding it, instead just make it visible?
Dynamic controls are always a little tricky in webforms.
Try creating a button in you case 2.
If you're dynamically creating controls in WebForms, you always have to recreate them on every postback and before the ViewState is loaded. Otherwise you end up with corrupt/broken ViewState. Also, I believe events need to be attached at the latest in the Page.OnLoad(EventArgs e) for them to fire.
From the example you posted, it doesn't look like the button is dynamic; it looks like you're just assigning the event handler dynamically. If that is the case, you don't need to reassign the event handler every time the page posts back. For that matter, you shouldn't be reassigning the ID either if it's already defined in the markup.
Try this:
if (!Page.IsPostBack)
{
Button1.Click += new EventHandler(Button1_Click);
}
EDIT
From the looks of your code, the right way to handle your situation would be to put the control in the panel to start with, and toggle the visiblity of the panel when the selected index of the dropdown changes.
protected void DropDown1_SelectedIndexChanged(object sender, EventArgs e)
{
Panel1.Visible = SomeIntValue == 2;
}
Per your current code:
If I create a button in the markup, like this:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" ...>
And in the code behind, I attempt to move the control to another panel, like this:
SomePanel.Controls.Add(Button1);
That is not the right way to do what you're looking to do, but technically speaking the ID (on the server) and the event handler would stay intact regardless of where you attempt to move the control to.
精彩评论