Dynamically add linkbuttons and wire them to same event??

Discussion in 'ASP .Net Building Controls' started by Linda, Apr 28, 2004.

  1. Linda

    Linda Guest

    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
     
    Linda, Apr 28, 2004
    #1
    1. Advertising

  2. 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
    "Linda" <> wrote in message news:%...
    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
     
    Victor Garcia Aprea [MVP], Apr 28, 2004
    #2
    1. Advertising

  3. 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" <>
    wrote:

    >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
     
    Robert Verderber, Apr 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,970
  2. Chu
    Replies:
    2
    Views:
    909
    Winista
    Apr 10, 2006
  3. RSH
    Replies:
    1
    Views:
    406
  4. David
    Replies:
    5
    Views:
    274
    Jeffrey Tan[MSFT]
    Feb 18, 2004
  5. rifat yavuz
    Replies:
    0
    Views:
    219
    rifat yavuz
    Jul 4, 2008
Loading...

Share This Page