Search code examples
c#.netbuttonbuttonclickpreinit

C# / apsx.net - Dynamically created button doesn't work


Here is my problem: I had dynamically created some buttons in my page (in the Page_PreInit method), all linked to the same event handler. But those buttons don't fire the event when I click on them... Can someone help me?

Here is some of my code:

Button creation (on a foreach loop on the Page_PreInit method):

Button b = new Button();
field.Controls.Add(b);

b.Text = "Download";
b.ID = tmp_out[type] as String;
b.Click += new EventHandler(Download_Click);

The OnClick method:

private void Download_Click(object sender, EventArgs e)
{
    //doing some stuff
}

Solution

  • Ok I solved my problem.

    The ids of the buttons was containing some '\'. I just removed those '\' and it works just fine.

    Thanks all for your reply!