• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1077
  • Last Modified:

add click event to control created at runtime

how to add a click event to a imagebutton created at runtime.

        Dim imgbtnHome As New ImageButton
        imgbtnHome.ImageUrl = "~/Images/btnHome.gif"
        form1.Controls.Add(imgbtnHome)
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
        Response.Redirect("default.aspx")
    End Sub
0
LeeHopkins
Asked:
LeeHopkins
2 Solutions
 
frodomanCommented:
You have to add your event handler at runtime also if that's when you're creating the control.  See an example here: http://msdn2.microsoft.com/en-us/library/ms743596.aspx

0
 
TSmoothCommented:
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)
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now