add click event to control created at runtime

Posted on 2007-10-04
Last Modified: 2012-05-05
how to add a click event to a imagebutton created at runtime.

        Dim imgbtnHome As New ImageButton
        imgbtnHome.ImageUrl = "~/Images/btnHome.gif"
I am getting an error if i try this

    Protected Sub imgbtnhome_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnhome.Click
    End Sub
Question by:LeeHopkins
    LVL 42

    Accepted Solution

    You have to add your event handler at runtime also if that's when you're creating the control.  See an example here:

    LVL 16

    Assisted Solution

    Get rid of the "Handles imgbtnhome.Click" part of your event handler definition and then add the following line of code where you create the image button:

    AddHandler imgbtnHome.Click, New ImageClickEventHandler(AddressOf imgbtnhome_Click)
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now