We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

How do I add an onclick event to a dynamically created button in C#/ASP.NET?

digitalconvict
on
Medium Priority
849 Views
Last Modified: 2012-05-05
I have the following method:

            private void RenderActionTable()
            {
                  User user = GetCurrentUser();
                  if(!user.IsAdmin)
                        return;

                  TableRow row = new TableRow();
                  Button btnAdmin = new Button();
                  btnAdmin.Text = "Admin";
                  btnAdmin.Click += new EventHandler(btnAdmin_Click);
                  btnAdmin.ID = "btnAdmin";
                  TableCell cell1 = new TableCell();
                  cell1.Controls.Add(btnAdmin);
                  row.Cells.Add(cell1);
                  actionTable.Rows.Add(row);
            }

Note: actionTable is a preExisting Table control.

If I call this method from Page_Load() or OnInit(), the button renders just fine, but it never has the eventHandler linked to it.  All I see in the rendered HTML is:

<table id="actionTable" class="actionTable" border="0">
      <tr>
            <td><input type="submit" name="btnAdmin" value="Admin" id="btnAdmin" /></td>
      </tr>
</table>

So why does ASP.NET not add the button event handler ("btnAdmin_Click") that I defined when it renders the button control?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Where is RenderActionTable called?

Bob

Author

Commented:
As I said, I tried calling it from both the Page_Load() and OnInit() methods respectively, but while the button renders ok, there is no postback event connected to it.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Did you try Page_PreRender?

Bob

Author

Commented:
I did now, it's no different from when I call the method from Page_Load or OnInit
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What .NET framework version are you using?  2.0, 1.1, 1.0?

I need to know how to reproduce this.

Bob

Author

Commented:
1.1

Commented:
just in the sake of experiment, try to add this button not to you table, but a panel or place holder and see if it's going to work - technically, your code looks good to me, just need to find out whether it's related to the table or not.

regs,
yurich

Author

Commented:
makes no odds, used a placeholder but the effect is no different :(

Author

Commented:
I found it.  When the page was processing a postback it was redirecting back to itself and thus never completed the initial PageLoad.  Thanks for all your diligent responses though.

Commented:
that must have been something like that as placing your code in page_load, oninit, or pre-render should not matter...

glad you sorted it out.
Commented:
PAQed with points refunded (500)

GranMod
Community Support Moderator

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.