dynamically adding buttons to an ASP page

  • Thread starter Thread starter Patrick
  • Start date Start date
P

Patrick

Hello,

I have an ASP page, where I want to add buttons dynamically.
I used C# and a for-loop do to this. The button generating code looks
like this:

Button bt1 = new Button();
bt1.Text = "Download";
bt1.ID = "" + id;
bt1.Click += new EventHandler(download_Click);
Placeholder1.Controls.Add(bt1);

The code worked fine for me until I embedded this page into a
Masterpage.
Now nothing happens when the button is clicked.

The method "download_Click" is never called.

How can I fix this?
BTW I noticed that the client_id and the uniqueID of the button change,
after it is added to the placeholder.

TIA,
Patrick
 
Make sure that the button is getting readded on postbabk during or before
page_load. Dynamically added controls aren't automagically readded on
postback, but they need to be there and their events hooked in order for
everything to work.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top