Raising an event from a user control

R

RSH

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 EmployeeNavigation

Inherits System.Web.UI.UserControl

Public Event GeneralInfoClicked(ByVal sender As System.Object, ByVal e
As System.Web.UI.ImageClickEventArgs)



' This fires when the imagebutton is clickedPrivate Sub
GeneralInfo_Clicked(ByVal sender As System.Object, ByVal e As
System.Web.UI.ImageClickEventArgs) Handles GeneralInfo.Click

RaiseEvent GeneralInfoClicked(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 EmployeeNavigation As EmployeeNavigation

Private Sub GeneralInfo_Clicked(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs) Handles
EmployeeNavigation.GeneralInfoClicked

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



End Sub
 
P

Phill W.

RSH said:
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 EmployeeNavigation
Inherits System.Web.UI.UserControl

Public Event GeneralInfoClicked( _
ByVal sender As System.Object _
, ByVal e As System.Web.UI.ImageClickEventArgs _
)

' This fires when the imagebutton is clicked
Private Sub GeneralInfo_Clicked( _
ByVal sender As System.Object _
, ByVal e As System.Web.UI.ImageClickEventArgs _
) Handles GeneralInfo.Click
RaiseEvent GeneralInfoClicked(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 EmployeeNavigation As EmployeeNavigation

EmployeeNavigation 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 EmployeeNavigation
Inherits System.Web.UI.UserControl

' This is the event exposed by this class (and its subclasses)
Public Event GeneralInfoClicked( _
ByVal sender As System.Object _
, ByVal e As System.Web.UI.ImageClickEventArgs _
)

' This fires when the imagebutton is clicked
' It uses the Protected method to raise the event.
Private Sub GeneralInfo_Clicked( _
ByVal sender As System.Object _
, ByVal e As System.Web.UI.ImageClickEventArgs _
) Handles GeneralInfo.Click

Me.OnGeneralInfoClicked(e)

End Sub

' This routine actually raises the event
' Subclasses can call this directly to get the event raised
Protected Sub OnGeneralInfoClicked( _
e As System.Web.UI.ImageClickEventArgs _
)

RaiseEvent GeneralInfoClicked(Me, e)

End Sub

End Class

Public Class SomeOtherEmployeeNavigation
Inherits EmployeeNavigation

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

End Class

Then, in your aspx code:

Protected WithEvents navigator As SomeOtherEmployeeNavigation


HTH,
Phill W.
 
R

RSH

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top