You need to create your own event in the user control. Then you need to raise that event when the button control inside the user control is clicked. Here is the code for the user control class codebehind:
Public Event MyButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs)
Sub MyButton_OnClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Clicked
RaiseEvent MyButtonClicked(Me, New System.EventArgs)
End Sub
Then the ASPX page class has to have a handler for that event. It also needs to have the user control declared as a class field such that it can handle the events and be visible throughout the class. Here is the code for the ASPX page class code behind:
Protected ucMyUserControl As MyUserControl
Sub Page_Load(...)
ucMyUserControl = CType(LoadControl("MyUserControl.ascx"), MyUserControl)
'Add the control where you need to here with ...Controls.Add(ucMyUserControl)
End Sub
Sub MyUserControlButton_OnClick(...) Handles ucMyUserControl.MyButtonClicked
'Handle the button click here
End Sub
Here's the sequence of events:
1. Page loads
1a. Page loads the user control
2. Events in the page and controls happen
2a. Button control "Click" event handler catches button click. It Raises the user control's MyButtonClicked event.
2b. Back in the ASPX, the user control's "MyButtonClick" event is handled by the MyUserControlButton_OnClick sub.
If you need to access items within the user control, you'll need to provide public access to them. A more elegant way of dealing with this need, however, is to utilize the event args argument of the event. If you would like more information about that, please post again. That subject is really another topic for discussion.
Peter
------------------------------------------------------
Work smarter, not harder.
|