1

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();
}

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

2 Comments

I had a feeling is what I am going to end up going with is changing the visibility properties. I Was hoping there was a way to get to work dynamically but seems to be more frustrating than necessary.
I wish I could upvote this answer more than once. Hide/show or enable/disable operations are far easier to manage in ASP.NET than dynamic controls.
0

Try creating a button in you case 2.

Comments

0

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.

Comments

0

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.

2 Comments

Its dynamically added in my switch statement and that will still produce the same result.
It is not dynamically added in the switch. It is dynamically moved into the panel, but the button itself is not being dynamically created in the switch statement, unless you omitted some code. You should not have to reassign the ID or the event handler if that's all your code is doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.