Solved Dynamically add code behind a button

Posted on 2013-11-10
Last Modified: 2013-11-10
Hi. I use the following code to add a button dynamically to a web page.
Is it possible to add code behind the button click?

Dim btn As New Button
        btn.Text = "Push me!"
Question by:murbro
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 63

Expert Comment

by:Fernando Soto
ID: 39637183
Hi murbro;

This should give you what you need. Use the AddHandler to add a click event handler to btn and give it the address of the event handler you want to give to the btn object as shown below.

Dim btn As New Button
btn.Text = "Push me!"
AddHandler btn.Click, AddressOf HandlerName

' Event handler needs the correct signature for a button click event
Private Sub HandlerName(sender As Object, e As System.EventArgs)

' Handler code goes here

End Sub

Open in new window


Author Comment

ID: 39637191
Hi. Thanks. What code would I use to add the handler
LVL 63

Accepted Solution

Fernando Soto earned 500 total points
ID: 39637199

This line of code adds the handler.

AddHandler btn.Click, AddressOf HandlerName

Where HandlerName is the name of the Sub which handles the Click event.

Author Closing Comment

ID: 39637203
Thanks  very much
LVL 63

Expert Comment

by:Fernando Soto
ID: 39637205
Not a problem, glad to help.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 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