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

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?
digitalconvictAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Where is RenderActionTable called?

Bob
0
digitalconvictAuthor 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.
0
Bob LearnedCommented:
Did you try Page_PreRender?

Bob
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

digitalconvictAuthor Commented:
I did now, it's no different from when I call the method from Page_Load or OnInit
0
Bob LearnedCommented:
What .NET framework version are you using?  2.0, 1.1, 1.0?

I need to know how to reproduce this.

Bob
0
digitalconvictAuthor Commented:
1.1
0
YurichCommented:
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
0
digitalconvictAuthor Commented:
makes no odds, used a placeholder but the effect is no different :(
0
digitalconvictAuthor 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.
0
YurichCommented:
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.
0
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.