?
Solved

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

Posted on 2013-11-10
5
Medium Priority
?
1,044 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:Murray Brown
[X]
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
5 Comments
 
LVL 64

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:Murray Brown
ID: 39637191
Hi. Thanks. What code would I use to add the handler
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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:Murray Brown
ID: 39637203
Thanks  very much
0
 
LVL 64

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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