Dynamically add linkbuttons and wire them to same event??

L

Linda

Hi,

How do I dynamically add linkbuttons and wire them to same event? I am able to add linkbuttons but they do not fire the event. Does anybody have a working sample?



Many thanks,

Linda
 
V

Victor Garcia Aprea [MVP]

Hi Linda,

If their events are not firing then I'm guessing you may be adding them too late in the execution lifecycle. At which point are you adding them? Also any short sample code you could provide will greatly help the guessing game.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx

To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
Hi,

How do I dynamically add linkbuttons and wire them to same event? I am able to add linkbuttons but they do not fire the event. Does anybody have a working sample?



Many thanks,

Linda
 
R

Robert Verderber

Linda,

You have to add an event handler to the control that you added
dynamically. For the below example code I created a new ASP.NET
project and placed a PlaceHolder control on the form.

In the Page_Load event place the following code:

'Instance a link button
Dim MyLinkButton As New LinkButton

'Set the text
MyLinkButton.Text = "Click Here for Google"

'Dynamically add to Place Holder
PlaceHolder1.Controls.Add(MyLinkButton)

'Add an event handler to button
AddHandler MyLinkButton.Click, AddressOf HandleMyLinkButtonClick


Then create a sub (delagate) to handle the click. It must have the
parameters as in the example. This example redirects to Google.

Sub HandleMyLinkButtonClick(ByVal sender As Object, ByVal e As
System.EventArgs)
Response.Redirect("http://www.google.com")
End Sub


If you have multiple links pointing to the same Handler you could use
something like the following to determine which link was clicked:

SelectedItemText = CType(sender, LinkButton).Text


Hope that helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top