add onclick event handler to linkbutton

Posted on 2005-04-22
Last Modified: 2008-03-04
i am making a linkbutton dynamic via code ..

dim lnkBtn as new LinkButton

how can I attach a BindGrid() function to the lnkBtn when it is clicked ...

I know you can use OnClick when you declare <asp:LinkButton> in the .aspx page, but i'm dynamically creating the linkbutton and adding it to a control based on certain criteria .. so it's going to call a generic function.

Question by:SeanGraflund
    LVL 23

    Expert Comment

    AddHandler lnkBtn.Click, Address lnkBtn_Click

    and have the following;

    Private Sub lnkBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
       ' code
    End Sub

    Notice, there's no Handles lnkBtn.Click
    LVL 23

    Expert Comment


    AddHandler lnkBtn.Click, AddressOf lnkBtn_Click
    LVL 1

    Author Comment


    it doesn't work .. i set a breakpoint at the code, it adds the handler fine when building, then i click on it, it doesn't go to the function, it just loopes through page_load as normal
    LVL 23

    Accepted Solution

    where are you adding the dynamic button that's the problem...
    upon postback if the button isnt there, the event wont fire..
    LVL 4

    Expert Comment

    Indeed, use lnkBtn.Command instead of .Click.
    Remember to add your own (unique) .ID when creating lnkBtn and recreate the link again at Page_Load time for the link to work.
    Also, you need to add for the AddHandler statement.
        Private WithEvents lnkBtn As System.Web.UI.WebControls.LinkButton

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 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

    13 Experts available now in Live!

    Get 1:1 Help Now