Solved

ASP.net VB.net Dynamically add code behind a button

Posted on 2013-11-10
5
921 Views
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!"
        Panel1.Controls.Add(btn)
0
Comment
Question by:murbro
  • 3
  • 2
5 Comments
 
LVL 62

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
Panel1.Controls.Add(btn)

' 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

0
 

Author Comment

by:murbro
ID: 39637191
Hi. Thanks. What code would I use to add the handler
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39637199
Hi,

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.
0
 

Author Closing Comment

by:murbro
ID: 39637203
Thanks  very much
0
 
LVL 62

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now