Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-03-28
12
Medium Priority
?
815 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?
0
Comment
Question by:digitalconvict
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16314751
Where is RenderActionTable called?

Bob
0
 

Author Comment

by:digitalconvict
ID: 16314970
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16315023
Did you try Page_PreRender?

Bob
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:digitalconvict
ID: 16315187
I did now, it's no different from when I call the method from Page_Load or OnInit
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16315424
What .NET framework version are you using?  2.0, 1.1, 1.0?

I need to know how to reproduce this.

Bob
0
 

Author Comment

by:digitalconvict
ID: 16315475
1.1
0
 
LVL 21

Expert Comment

by:Yurich
ID: 16317311
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
 

Author Comment

by:digitalconvict
ID: 16317395
makes no odds, used a placeholder but the effect is no different :(
0
 

Author Comment

by:digitalconvict
ID: 16317442
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
 
LVL 21

Expert Comment

by:Yurich
ID: 16317950
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
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16560799
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question