Raising Events on a UserControl that are Handled by a WebForm
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngbeta' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Patrick Barnes (VIP)
I'm trying to raise an event on a usercontrol that gets handled on the
webform that consumes usercontrol. I dynamically load the usercontrol in the
Page_Load event. I cannot figure out the syntax to handle this event on the
WebForm. Here is the relevant code from my usercontrol:

    Public Delegate Sub ucBN_UpdateEventHandler(ByVal sender As Object, ByVal e
As EventArgs)

    Public Event ucBN_Update As ucBN_UpdateEventHandler

    Sub BN_Update_Click(ByVal sender As Object, ByVal e As EventArgs)

        RaiseEvent ucBN_Update(sender, e)

    End Sub

I'm assuming this is on the right track, although I may be off here. I want
my button click eventhandler on the WebForm to declaratively handle this
usercontrol event. I'm pretty sure I need to use the Handles keyword with
this button click event, i.e., something like:

    Public Sub BN_Update_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles my_ucCookieUI.ucBN_Update

I get an error, however, stating that "the Handles clause requires
WithEvents variable as qualifier." I try to add "With Events" to my
declaration, but still have errors. So I figure it maybe has something to do
with how I declare my usercontrol. This is what I have in the WebForm
codebehind class declaration for my dynamically loaded usercontrol:

    Dim myControl As Control = Page.LoadControl("/controls/cookieui.ascx")
    Dim my_ucCookieUI As ucCookieUI = CType(myControl, ucCookieUI)

I tried this syntax for the second line, but got errors:

    Protected WithEvents my_ucCookieUI As ucCookieUI
    my_ucCookieUI = CType(myControl, ucCookieUI)

Can anyone help me out here?

Patrick Barnes
Web Application Developer
Geonetric Technologies
200 1st Avenue NE Suite 220
Cedar Rapids, Iowa 52401
P:319.221.1667 / F:319.221.1450 / www.geonetric.com

Reply to this message...
 
    
Michael Gaertner (VIP)
I may have sent you this already, but this post from Susan Warren has code
to raise events from controls. I used it to raise events from a child
control to an enclosing parent control.

Good luck.

Michael

Did you know you can raise events from a user control? Here's a little
sample, LabelButton.ascx (essentially the equivalent of asp:LinkButton),
that demonstrates implementing a postback event like click. Controls that
fires postback events must implement the IPostBackEventHandler interface --
see the first directive of the user control. Cool, huh?

LabelButton.ascx

[Original message clipped]

| [aspngcontrolscs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

-----Original Message-----
From: Patrick Barnes [mailto:Click here to reveal e-mail address]
Sent: Friday, May 04, 2001 11:00 AM
To: aspngbeta
Subject: [aspngbeta] Raising Events on a UserControl that are Handled by
a WebForm

I'm trying to raise an event on a usercontrol that gets handled on the
webform that consumes usercontrol. I dynamically load the usercontrol in the
Page_Load event. I cannot figure out the syntax to handle this event on the
WebForm. Here is the relevant code from my usercontrol:

    Public Delegate Sub ucBN_UpdateEventHandler(ByVal sender As Object, ByVal e
As EventArgs)

    Public Event ucBN_Update As ucBN_UpdateEventHandler

    Sub BN_Update_Click(ByVal sender As Object, ByVal e As EventArgs)

        RaiseEvent ucBN_Update(sender, e)

    End Sub

I'm assuming this is on the right track, although I may be off here. I want
my button click eventhandler on the WebForm to declaratively handle this
usercontrol event. I'm pretty sure I need to use the Handles keyword with
this button click event, i.e., something like:

    Public Sub BN_Update_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles my_ucCookieUI.ucBN_Update

I get an error, however, stating that "the Handles clause requires
WithEvents variable as qualifier." I try to add "With Events" to my
declaration, but still have errors. So I figure it maybe has something to do
with how I declare my usercontrol. This is what I have in the WebForm
codebehind class declaration for my dynamically loaded usercontrol:

    Dim myControl As Control = Page.LoadControl("/controls/cookieui.ascx")
    Dim my_ucCookieUI As ucCookieUI = CType(myControl, ucCookieUI)

I tried this syntax for the second line, but got errors:

    Protected WithEvents my_ucCookieUI As ucCookieUI
    my_ucCookieUI = CType(myControl, ucCookieUI)

Can anyone help me out here?

Patrick Barnes
Web Application Developer
Geonetric Technologies
200 1st Avenue NE Suite 220
Cedar Rapids, Iowa 52401
P:319.221.1667 / F:319.221.1450 / www.geonetric.com

| [aspngbeta] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT

Reply to this message...
 
    
Stuart C. Salsbury
For a simple example of raising events from a constituent control (much the
same as an .ascx file), see:

http://docs.aspng.com/quickstart/aspplus/doc/webctrlauthoring.aspx#exposinge
vts

Regards,
Stuart Salsbury
Ernst & Young LLP

-----Original Message-----
From: Patrick Barnes
To: aspngbeta
Sent: 5/4/2001 11:00 AM
Subject: [aspngbeta] Raising Events on a UserControl that are Handled by a
WebForm

I'm trying to raise an event on a usercontrol that gets handled on the
webform that consumes usercontrol. I dynamically load the usercontrol in
the
Page_Load event. I cannot figure out the syntax to handle this event on
the
WebForm. Here is the relevant code from my usercontrol:

Public Delegate Sub ucBN_UpdateEventHandler(ByVal sender As
Object, ByVal e
As EventArgs)

Public Event ucBN_Update As ucBN_UpdateEventHandler

Sub BN_Update_Click(ByVal sender As Object, ByVal e As
EventArgs)

RaiseEvent ucBN_Update(sender, e)

End Sub

I'm assuming this is on the right track, although I may be off here. I
want
my button click eventhandler on the WebForm to declaratively handle this
usercontrol event. I'm pretty sure I need to use the Handles keyword
with
this button click event, i.e., something like:

Public Sub BN_Update_Click(ByVal sender As System.Object, ByVal
e As
System.EventArgs) Handles my_ucCookieUI.ucBN_Update

I get an error, however, stating that "the Handles clause requires
WithEvents variable as qualifier." I try to add "With Events" to my
declaration, but still have errors. So I figure it maybe has something
to do
with how I declare my usercontrol. This is what I have in the WebForm
codebehind class declaration for my dynamically loaded usercontrol:

Dim myControl As Control =
Page.LoadControl("/controls/cookieui.ascx")
Dim my_ucCookieUI As ucCookieUI = CType(myControl, ucCookieUI)

I tried this syntax for the second line, but got errors:

Protected WithEvents my_ucCookieUI As ucCookieUI
my_ucCookieUI = CType(myControl, ucCookieUI)

Can anyone help me out here?

Patrick Barnes
Web Application Developer
Geonetric Technologies
200 1st Avenue NE Suite 220
Cedar Rapids, Iowa 52401
P:319.221.1667 / F:319.221.1450 / www.geonetric.com

Reply to this message...
 
 
System.EventArgs
System.EventHandler
System.Messaging.Message
System.Object
System.String
System.Web.UI.IPostBackEventHandler
System.Web.UI.Page
System.Web.UI.UserControl
System.Web.UI.WebControls.LinkButton
System.Windows.Forms.UserControl




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com