|
 |
aspx_beginners thread: Events and Delegates
Message #1 by briley@c... on Sun, 2 Mar 2003 00:41:48
|
|
I'm having a problem getting an event to fire in a usercontrol, which
initiates a subroutine in the page where the usercontrol resides. When I
execute the page, I receive no errors, but I'm not getting the results I
would expect. What am I doing wrong? (I have omitted most excess code
for breviaty)
<Code>
--Usercontrol (selectarea) codebehind
Public MustInherit Class SelectArea
Public Event MyEvent As MyEventDelegate
Public Delegate Sub MyEventDelegate(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Sub btnSelectArea_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSelectArea.Click
Dim anItem As DataListItem
Dim blnIsChecked As Boolean
Dim intCheckedArea As Integer
Dim colSelectedArea As ArrayList
Dim chkSelected As System.Web.UI.WebControls.CheckBox
colSelectedArea = New ArrayList()
For Each anItem In dlstActiveAreas.Items
chkSelected = anItem.FindControl("chkAreaChoice")
If chkSelected.Checked Then
intCheckedArea = dlstActiveAreas.DataKeys
(anItem.ItemIndex)
colSelectedArea.Add(intCheckedArea)
End If
Next
RaiseEvent MyEvent(Me, e)
End Sub
End Class
---Page object (codebehind)
Public Class ViewAllBids
Inherits System.Web.UI.Page
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim objMyUserControl As SelectArea
objMyUserControl = CType(LoadControl
("UserControls/SelectArea.ascx"), SelectArea)
AddHandler objMyUserControl.MyEvent, AddressOf ucSelectArea
End Sub
Private Sub ucSelectArea(ByVal sender As System.Object, ByVal e As
System.EventArgs)
lblMessage.Text = "btnSelectArea Clicked"
End Sub
End Class
</Code>
I figure, if this were working properly, I would see "btnSelectArea
Clicked" in the output on my webform.
Can anyone see what I've done wrong?
Message #2 by "Peter Lanoie" <planoie@n...> on Tue, 4 Mar 2003 15:24:31 -0500
|
|
Bob,
Your code looks superb, but you are missing one piece:
In page_load(), you are instantiating the user control, and hooking in the
event. However, what are you doing with the user control? After you load
the control, you need to get it on the page. Add the following to
page_load() after you setup the user control:
Me.Controls.Add(objMyUserControl)
That should take care of it for you.
Peter
-----Original Message-----
From: briley@c... [mailto:briley@c...]
Sent: Sunday, March 02, 2003 00:42
To: aspx_beginners
Subject: [aspx_beginners] Events and Delegates
I'm having a problem getting an event to fire in a usercontrol, which
initiates a subroutine in the page where the usercontrol resides. When I
execute the page, I receive no errors, but I'm not getting the results I
would expect. What am I doing wrong? (I have omitted most excess code
for breviaty)
<Code>
--Usercontrol (selectarea) codebehind
Public MustInherit Class SelectArea
Public Event MyEvent As MyEventDelegate
Public Delegate Sub MyEventDelegate(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Sub btnSelectArea_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSelectArea.Click
Dim anItem As DataListItem
Dim blnIsChecked As Boolean
Dim intCheckedArea As Integer
Dim colSelectedArea As ArrayList
Dim chkSelected As System.Web.UI.WebControls.CheckBox
colSelectedArea = New ArrayList()
For Each anItem In dlstActiveAreas.Items
chkSelected = anItem.FindControl("chkAreaChoice")
If chkSelected.Checked Then
intCheckedArea = dlstActiveAreas.DataKeys
(anItem.ItemIndex)
colSelectedArea.Add(intCheckedArea)
End If
Next
RaiseEvent MyEvent(Me, e)
End Sub
End Class
---Page object (codebehind)
Public Class ViewAllBids
Inherits System.Web.UI.Page
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim objMyUserControl As SelectArea
objMyUserControl = CType(LoadControl
("UserControls/SelectArea.ascx"), SelectArea)
AddHandler objMyUserControl.MyEvent, AddressOf ucSelectArea
End Sub
Private Sub ucSelectArea(ByVal sender As System.Object, ByVal e As
System.EventArgs)
lblMessage.Text = "btnSelectArea Clicked"
End Sub
End Class
</Code>
I figure, if this were working properly, I would see "btnSelectArea
Clicked" in the output on my webform.
Can anyone see what I've done wrong?
Message #3 by "Bob Riley at cars4U.com" <BRiley@c...> on Tue, 4 Mar 2003 15:30:16 -0500
|
|
Thanks. I didn't realize you were responding to my thread. I played
around with the code that you sent through over the weekend and I got it
working.
-----Original Message-----
From: Peter Lanoie [mailto:planoie@n...]
Sent: Tuesday, March 04, 2003 3:25 PM
To: aspx_beginners
Cc: Bob Riley at cars4U.com
Subject: [aspx_beginners] RE: Events and Delegates
Bob,
Your code looks superb, but you are missing one piece:
In page_load(), you are instantiating the user control, and hooking in
the
event. However, what are you doing with the user control? After you
load
the control, you need to get it on the page. Add the following to
page_load() after you setup the user control:
Me.Controls.Add(objMyUserControl)
That should take care of it for you.
Peter
-----Original Message-----
From: briley@c... [mailto:briley@c...]
Sent: Sunday, March 02, 2003 00:42
To: aspx_beginners
Subject: [aspx_beginners] Events and Delegates
I'm having a problem getting an event to fire in a usercontrol, which
initiates a subroutine in the page where the usercontrol resides. When
I
execute the page, I receive no errors, but I'm not getting the results I
would expect. What am I doing wrong? (I have omitted most excess code
for breviaty)
<Code>
--Usercontrol (selectarea) codebehind
Public MustInherit Class SelectArea
Public Event MyEvent As MyEventDelegate
Public Delegate Sub MyEventDelegate(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Sub btnSelectArea_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSelectArea.Click
Dim anItem As DataListItem
Dim blnIsChecked As Boolean
Dim intCheckedArea As Integer
Dim colSelectedArea As ArrayList
Dim chkSelected As System.Web.UI.WebControls.CheckBox
colSelectedArea =3D New ArrayList()
For Each anItem In dlstActiveAreas.Items
chkSelected =3D anItem.FindControl("chkAreaChoice")
If chkSelected.Checked Then
intCheckedArea =3D dlstActiveAreas.DataKeys
(anItem.ItemIndex)
colSelectedArea.Add(intCheckedArea)
End If
Next
RaiseEvent MyEvent(Me, e)
End Sub
End Class
---Page object (codebehind)
Public Class ViewAllBids
Inherits System.Web.UI.Page
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim objMyUserControl As SelectArea
objMyUserControl =3D CType(LoadControl
("UserControls/SelectArea.ascx"), SelectArea)
AddHandler objMyUserControl.MyEvent, AddressOf ucSelectArea
End Sub
Private Sub ucSelectArea(ByVal sender As System.Object, ByVal e As
System.EventArgs)
lblMessage.Text =3D "btnSelectArea Clicked"
End Sub
End Class
</Code>
I figure, if this were working properly, I would see "btnSelectArea
Clicked" in the output on my webform.
Can anyone see what I've done wrong?
|
|
 |