Home All Groups Group Topic Archive Search About

Raising an event from a user control

Author
18 Dec 2006 9:20 PM
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

Author
19 Dec 2006 10:45 AM
Phill W.
RSH wrote:

Show quoteHide quote
> 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.
Author
19 Dec 2006 1:31 PM
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

Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> wrote in message
news:em8fsi$b3l$1@south.jnrs.ja.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 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.