|
 |
aspx_beginners thread: RE: UserControl and Page Events
Message #1 by "Peter Lanoie" <planoie@n...> on Tue, 4 Mar 2003 15:23:26 -0500
|
|
Too bad good-will code help doesn't pay so well! Maybe Wrox should set up a
"Rate this answer" feedback form on the emails. When you reach X rating
points, you get a free Wrox Book! Wouldn't THAT motivate people to help
out?!
Peter
-----Original Message-----
From: briley@c... [mailto:briley@c...]
Sent: Friday, February 28, 2003 21:44
To: aspx_beginners
Subject: [aspx_beginners] RE: UserControl and Page Events
You must do this for a living! Accolades abound for your reply!
Cheers
Bob Riley
> 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
Hope this lengthy explanation is helpful.
Peter
-----Original Message-----
From: briley@c... [mailto:briley@c...]
Sent: Friday, February 28, 2003 05:32
To: aspx_beginners
Subject: [aspx_beginners] UserControl and Page Events
When a button is pressed in a usercontrol - is it also possible to have a
sub routine that handles some code for this event in the main page as
well?
I have a UserControl that displays a checklist in a datalist. A button
on the usercontrol records the checked items in a datalist and stores
them in an arraylist. Depending on which page the usercontrol resides
in - different code needs to be executed. Is it possible - when the
button is pressed in the usercontrol - to have an event fired in both in
the usercontrol, and on the parent page?
Message #2 by "Bob Riley at cars4U.com" <BRiley@c...> on Tue, 4 Mar 2003 15:35:37 -0500
|
|
Sure would be a good idea. I actually forwarded your sample off to a
couple of other people asking questions about raising an event from a
user control. You should receive points for that as well. You wouldn't
believe the lack of information on events and delegates (in VB). I
scoured my own library of books, Books 24x7, and the Internet - and most
of what I found was in C#. Seems that a lot of the more advanced topics
are discussed in C#.
-----Original Message-----
From: Peter Lanoie [mailto:planoie@n...]
Sent: Tuesday, March 04, 2003 3:23 PM
To: aspx_beginners
Subject: [aspx_beginners] RE: UserControl and Page Events
Too bad good-will code help doesn't pay so well! Maybe Wrox should set
up a
"Rate this answer" feedback form on the emails. When you reach X rating
points, you get a free Wrox Book! Wouldn't THAT motivate people to help
out?!
Peter
-----Original Message-----
From: briley@c... [mailto:briley@c...]
Sent: Friday, February 28, 2003 21:44
To: aspx_beginners
Subject: [aspx_beginners] RE: UserControl and Page Events
You must do this for a living! Accolades abound for your reply!
Cheers
Bob Riley
> 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 =3D 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 =3D CType(LoadControl("myFirstUC.ascx"), MyFirstUC)
objMySecondUC =3D 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
Hope this lengthy explanation is helpful.
Peter
-----Original Message-----
From: briley@c... [mailto:briley@c...]
Sent: Friday, February 28, 2003 05:32
To: aspx_beginners
Subject: [aspx_beginners] UserControl and Page Events
When a button is pressed in a usercontrol - is it also possible to have
a
sub routine that handles some code for this event in the main page as
well?
I have a UserControl that displays a checklist in a datalist. A button
on the usercontrol records the checked items in a datalist and stores
them in an arraylist. Depending on which page the usercontrol resides
in - different code needs to be executed. Is it possible - when the
button is pressed in the usercontrol - to have an event fired in both in
the usercontrol, and on the parent page?
|
|
 |