I had the same question last week. I received the Email below from Peter
Lanoie. It was a great help.
Note: Since your control is already loaded into the form, you won't need the
first two lines of code in the Load event of the page. I couldn't get the
function to work because of signature problems (still haven't figured this out)
- but when I replaced the function with a subroutine - everything worked well.
If you're having problems getting this to work after a bit - I'll send you my
sample that I was able to get working.
Let's consider this example (in VB):
We have an ASPX page "myPage.aspx" (Class myPage) and a user control
"myUserControl.ascx" (Class myUserControl). There is a function in the PAGE
(myPage.myPageFunction()) that wants to run when something happens in the
CONTROL. In the Control we need to define an event and a delegate for that
event. We can then raise this event from where ever we need within the
control, in your case it's the button click routine.
User Control file:
Class myUserControl
...
Public Event MyEvent As MyEventDelegate
Public Delegate Sub MyEventDelegate(ByVal sender As System.Object, ByVal e
As System.EventArgs)
Private Sub MyButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyButton.onClick
RaiseEvent MyEvent(Me, e)
End Sub
...
End Class
Now you have your event defined and raised. We need to tie this in to the
ASPX page that contains the control. Presumably, in page load we are
loading up the user control. Once we have an object for the user control,
we can hook a function in the PAGE into the event in the CONTROL with
"AddHandler".
ASPX Page:
Class myPage
...
Private Sub Page_Load(...)
...
Dim objMyUserControl As myUserControl
objMyUserControl = CType(LoadControl("myUserControl.ascx"),
myUserControl)
AddHandler objMyUserControl.MyEvent, AddressOf myPageFunction
...
End Sub
...
Private Function myPageFunction(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'Do whatever you need to in here.
End Function
...
End Class
One important thing to remember: Any function that you hook into an event
with must have the same signature as the event delegate. That's why
"myPageFunction" has "sender" and "e" as arguments. The delegate will pass
those along in the event.
Now when you press the button in the user control, the event triggers the
function call in the page. You can extend this functionality and create a
function in ANOTHER user control, and hook
events in two user controls
together. You do this in a similar fashion as we did in the ASPX
page_load(). Instead of hooking a page function to a user control event,
you just hook one UC function to the other UC event:
Dim objMyFirstUC as MyFirstUC
Dim objMySecondUC as MySecondUC
objMyFirstUC = CType(LoadControl("myFirstUC.ascx"), MyFirstUC)
objMySecondUC = CType(LoadControl("mySecondUC.ascx"), MySecondUC)
AddHandler objMyFirstUC.FirstUCEvent, AddressOf
objMySecondUC.DoFirstUCEvent
AddHandler objMySecondUC.SecondUCEvent, AddressOf
objMyFirstUC.DoSecondUCEvent
As you can see, you can go on and on, tying together controls to the page,
or to other controls.
Check out this article. It's basically what I discussed.
http://www.dotnetextreme.com/code/eventhandling.asp