Reputation: 1396
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.SelectedIndex)
{
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();
}
Upvotes: 1
Views: 11640
Reputation: 46077
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.
Upvotes: 0
Reputation: 6981
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.
Upvotes: 0
Reputation: 15722
This is a timing issue. Your program flow is like this:
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.
Upvotes: 8
Reputation: 31606
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.
Upvotes: 4