all groups > asp.net building controls > april 2004 >
You're in the

asp.net building controls

group:

Dynamically add linkbuttons and wire them to same event??


Dynamically add linkbuttons and wire them to same event?? Linda
4/27/2004 5:35:10 PM
asp.net building controls:
Hi,=20

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,

Re: Dynamically add linkbuttons and wire them to same event?? Victor Garcia Aprea [MVP]
4/27/2004 9:44:21 PM
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.

--=20
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
[quoted text, click to view]
Hi,=20

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,

Re: Dynamically add linkbuttons and wire them to same event?? Robert Verderber
4/28/2004 3:27:26 AM
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.


On Tue, 27 Apr 2004 17:35:10 -0700, "Linda" <linda@do-not-spam.com>
[quoted text, click to view]
AddThis Social Bookmark Button