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

How do you setup AddHandler on a parent form for a usercontrol event that is nested within a gridview?

I am trying to call a function using delegates in a parent form using AddHandler when a button click event is fired within a usercontrol that is on the form.

I have built a test application where I can get this working. The class on the parent form is:

Partial Class _Default
    Inherits System.Web.UI.Page
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        AddHandler WebUserControl1.btnHandler, AddressOf test
    End Sub

    Public Sub test(ByVal strVacancyID As String)
       Response.Write("Called From TEST: " + strVacancyID)
    End Sub
End Class

The class on the User Control is:

Partial Class usercontrol_WebUserControl
    Inherits System.Web.UI.UserControl

    Public Delegate Sub OnButtonClick(ByVal strVacancyID As String)

    Public Event btnHandler As OnButtonClick

    Protected Sub btnTest_Click(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent btnHandler("87")
    End Sub
End Class

However, in my current project the user control is nested within a gridview, when I try to add the "AddHandler" code the form can't "see" the user control, presumably because it is nested within the gridview.

How can I link the nested usercontrol to the AddHandler?

Many thanks,
1 Solution
Hi, you need to add the reference within the usercontrol tag:
<uc1:WebUserControl ID="WebUserControl1" runat="server" OnbtnHandler="test"/>

OnbtnHandler should appear in intellisense.
robert_rawAuthor Commented:
Hi vb jonas,
Thanks for the solution it works perfectly. I would never have figured that out myself. Thanks very much.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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