rachelreese's blog

One Rachii's take on .NET, Phoenix, and some unrelated, potentially completely random things.

Raising Events

I find myself answering a lot of questions from folks on the forums and various lists about how to raise an event from a user control to the main page it was in.  For posterity, I thought I'd post an example to handle a button OnClick event in here:

In your user control, declare a delegate with the relevant arguments & an event to handle it:
-------------------------------
Public Delegate Sub delOnclick(ByVal sender As System.Object, ByVal e
As System.EventArgs)
Public Event evtOnClick As delOnclick
-------------------------------

Add a sub in your user control that handles the button's onclick event, and in the sub raise the event you just created.
-------------------------------
Sub btnOnclick(sender as object, system as eventargs) handles
button.onclick
RaiseEvent evtOnclick(sender, e)
End Sub
-------------------------------

And then in your parent page, add code that handles the event in the page load:
-------------------------------
AddHandler YourUserControl.evtOnClick, AddressOf TheSubYouWantToCall
-------------------------------

That should do it.

Posted: Aug 11 2003, 10:07 AM by rachelreese | with no comments
Filed under:

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)