Raising an event from a user control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RSH

    #1

    Raising an event from a user control

    Hi,

    I have been trying to follow a tutorial on raising an event from a user
    control and then handling it in the parent page.

    The article is a little vague so I believe I have the code in place, but I
    cant seem to get it to work.

    Here is what I have:

    ' This is in the ascx control:
    ' GeneralInfo is an ImageButton in the ascx file
    Public MustInherit Class EmployeeNavigat ion

    Inherits System.Web.UI.U serControl

    Public Event GeneralInfoClic ked(ByVal sender As System.Object, ByVal e
    As System.Web.UI.I mageClickEventA rgs)



    ' This fires when the imagebutton is clickedPrivate Sub
    GeneralInfo_Cli cked(ByVal sender As System.Object, ByVal e As
    System.Web.UI.I mageClickEventA rgs) Handles GeneralInfo.Cli ck

    RaiseEvent GeneralInfoClic ked(sender, e)

    End Sub

    End Class





    This is excerpts of the code I have in the aspx page which inherits the ascx
    page above.

    Protected WithEvents EmployeeNavigat ion As EmployeeNavigat ion

    Private Sub GeneralInfo_Cli cked(ByVal sender As Object, ByVal e As
    System.Web.UI.I mageClickEventA rgs) Handles
    EmployeeNavigat ion.GeneralInfo Clicked

    Response.Write( "<BEVENT FIRED!</b>")



    End Sub


  • Phill W.

    #2
    Re: Raising an event from a user control

    RSH wrote:
    I have been trying to follow a tutorial on raising an event from a user
    control and then handling it in the parent page.
    >
    The article is a little vague so I believe I have the code in place, but I
    cant seem to get it to work.
    >
    Here is what I have:
    >
    Public MustInherit Class EmployeeNavigat ion
    Inherits System.Web.UI.U serControl
    >
    Public Event GeneralInfoClic ked( _
    ByVal sender As System.Object _
    , ByVal e As System.Web.UI.I mageClickEventA rgs _
    )
    >
    ' This fires when the imagebutton is clicked
    Private Sub GeneralInfo_Cli cked( _
    ByVal sender As System.Object _
    , ByVal e As System.Web.UI.I mageClickEventA rgs _
    ) Handles GeneralInfo.Cli ck
    RaiseEvent GeneralInfoClic ked(sender, e)
    End Sub
    >
    End Class
    >
    >
    This is excerpts of the code I have in the aspx page which inherits the ascx
    page above.
    >
    Protected WithEvents EmployeeNavigat ion As EmployeeNavigat ion
    EmployeeNavigat ion is defined as MustInherit, so you cannot create
    intances of it. I assume that you have a subclass somewhere that you
    haven't shown us.

    Since you're using inheritance, I would suggest handling the event this
    way:

    Public MustInherit Class EmployeeNavigat ion
    Inherits System.Web.UI.U serControl

    ' This is the event exposed by this class (and its subclasses)
    Public Event GeneralInfoClic ked( _
    ByVal sender As System.Object _
    , ByVal e As System.Web.UI.I mageClickEventA rgs _
    )

    ' This fires when the imagebutton is clicked
    ' It uses the Protected method to raise the event.
    Private Sub GeneralInfo_Cli cked( _
    ByVal sender As System.Object _
    , ByVal e As System.Web.UI.I mageClickEventA rgs _
    ) Handles GeneralInfo.Cli ck

    Me.OnGeneralInf oClicked(e)

    End Sub

    ' This routine actually raises the event
    ' Subclasses can call this directly to get the event raised
    Protected Sub OnGeneralInfoCl icked( _
    e As System.Web.UI.I mageClickEventA rgs _
    )

    RaiseEvent GeneralInfoClic ked(Me, e)

    End Sub

    End Class

    Public Class SomeOtherEmploy eeNavigation
    Inherits EmployeeNavigat ion

    ' Note: NO click code in here;
    ' that's encapsulated in the base class

    End Class

    Then, in your aspx code:

    Protected WithEvents navigator As SomeOtherEmploy eeNavigation


    HTH,
    Phill W.

    Comment

    • RSH

      #3
      Re: Raising an event from a user control

      Phil,

      Thanks a ton! That is good stuff!

      Actually the MustInherit was a mistake, I didn't need that and as it turned
      out naming the instance the same name as the class is what seemed to be
      flaking eveything out.

      But I learned a ton by your example and I appreciate your time!

      Ron

      "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
      news:em8fsi$b3l $1@south.jnrs.j a.net...
      RSH wrote:
      >
      >I have been trying to follow a tutorial on raising an event from a user
      >control and then handling it in the parent page.
      >>
      >The article is a little vague so I believe I have the code in place, but
      >I cant seem to get it to work.
      >>
      >Here is what I have:
      >>
      >Public MustInherit Class EmployeeNavigat ion
      > Inherits System.Web.UI.U serControl
      >>
      > Public Event GeneralInfoClic ked( _
      > ByVal sender As System.Object _
      > , ByVal e As System.Web.UI.I mageClickEventA rgs _
      > )
      >>
      > ' This fires when the imagebutton is clicked Private Sub
      >GeneralInfo_Cl icked( _ ByVal sender As System.Object _ , ByVal e As
      >System.Web.UI. ImageClickEvent Args _
      > ) Handles GeneralInfo.Cli ck
      > RaiseEvent GeneralInfoClic ked(sender, e)
      > End Sub
      >>
      >End Class
      >>
      >>
      >This is excerpts of the code I have in the aspx page which inherits the
      >ascx page above.
      >>
      >Protected WithEvents EmployeeNavigat ion As EmployeeNavigat ion
      >
      EmployeeNavigat ion is defined as MustInherit, so you cannot create
      intances of it. I assume that you have a subclass somewhere that you
      haven't shown us.
      >
      Since you're using inheritance, I would suggest handling the event this
      way:
      >
      Public MustInherit Class EmployeeNavigat ion
      Inherits System.Web.UI.U serControl
      >
      ' This is the event exposed by this class (and its subclasses)
      Public Event GeneralInfoClic ked( _
      ByVal sender As System.Object _
      , ByVal e As System.Web.UI.I mageClickEventA rgs _
      )
      >
      ' This fires when the imagebutton is clicked
      ' It uses the Protected method to raise the event.
      Private Sub GeneralInfo_Cli cked( _
      ByVal sender As System.Object _
      , ByVal e As System.Web.UI.I mageClickEventA rgs _
      ) Handles GeneralInfo.Cli ck
      >
      Me.OnGeneralInf oClicked(e)
      >
      End Sub
      >
      ' This routine actually raises the event
      ' Subclasses can call this directly to get the event raised
      Protected Sub OnGeneralInfoCl icked( _
      e As System.Web.UI.I mageClickEventA rgs _
      )
      >
      RaiseEvent GeneralInfoClic ked(Me, e)
      >
      End Sub
      >
      End Class
      >
      Public Class SomeOtherEmploy eeNavigation
      Inherits EmployeeNavigat ion
      >
      ' Note: NO click code in here;
      ' that's encapsulated in the base class
      >
      End Class
      >
      Then, in your aspx code:
      >
      Protected WithEvents navigator As SomeOtherEmploy eeNavigation
      >
      >
      HTH,
      Phill W.

      Comment

      Working...