|
 |
aspx thread: Can event raise by user control handled by its hosting page?
Message #1 by "Kan Yu Ting" <yu-ting.kan@p...> on Fri, 14 Jun 2002 02:09:30
|
|
Hi all,
Please help. I want to raise some events within user control (e.g pressing
a button in the toolbar). As the event handling vary from page to page, I
want to consunme the event in the hosting page. How can I do?
Thanks forany suggestions.
Kan
Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Fri, 14 Jun 2002 09:55:08 -0400
|
|
Kan,
You basically want to bubble up events to the containing Page
object. There are two ways to do this, I will detail one way, the other way
is OnBubbleEvent which works well for controls that are similiar to the
DataRepeater or iteritive display controls (where you want to bubble events
on a one to one basis).
Say you have a button named "delete" on your user control, and when
its clicked you want to raise an event in the parent called "OnDelete". In
the user control itself, not under any void or function:
---
// this is really not required since we're linking the event
// in the *.ascx below, but I include it for completeness
protected System.Web.UI.WebControls.Button delete;
---
In the *.ascx file we need to actually put this button somewhere:
---
<asp:Button Id="delete" RunAt="server" OnClick="delete_Click" Text="Delete"
/>
---
So now on our user control we have a button that when clicked fires
the event INSIDE of the user control. How do we get this event out to the
rest of the world (including the parent containter)?
In the code behind file we declare an event delegate. We will use a
standard EventHandler delegate:
---
// declare event so it can have delegates attached
// to it - this is empty!
public event System.EventHandler Delete;
// add an event handler to check and see if the user attached
// an event; if so, fire it.
protected virtual void OnDelete(object sender, System.EventArgs e)
{
if (this.Delete != null)
Delete(this, e);
}
// this event is fired from our user control - use it
// to fire any delegates added to Delete
protected void Delete_Click(object sender, EventArgs e)
{
this.OnDelete(sender, e);
}
---
So now any page that uses the user control can wire up that event
(in *.aspx):
---
<Example:MyUserControl Id="blah" OnDelete="blah_Delete" />
---
HTH,
- Chuck
-----Original Message-----
From: Kan Yu Ting [mailto:yu-ting.kan@p...]
Sent: Thursday, June 13, 2002 10:10 PM
To: ASP+
Subject: [aspx] Can event raise by user control handled by its hosting
page?
Hi all,
Please help. I want to raise some events within user control (e.g pressing
a button in the toolbar). As the event handling vary from page to page, I
want to consunme the event in the hosting page. How can I do?
Thanks forany suggestions.
Kan
Message #3 by "Ray Jezek (TT)" <ray.jezek@t...> on Fri, 14 Jun 2002 11:23:32 -0500
|
|
I just coded one of these today. I found the following article very
helpful:
http://www.oreillynet.com/pub/a/dotnet/excerpt/progaspdotnet_14/index1.html?
page=3
-----Original Message-----
From: Kan Yu Ting [mailto:yu-ting.kan@p...]
Sent: Thursday, June 13, 2002 9:10 PM
To: ASP+
Subject: [aspx] Can event raise by user control handled by its
hosting page?
Hi all,
Please help. I want to raise some events within user control (e.g pressing
a button in the toolbar). As the event handling vary from page to page, I
want to consunme the event in the hosting page. How can I do?
Thanks forany suggestions.
Kan
Message #4 by "Kan Yu Ting" <yu-ting.kan@p...> on Sat, 15 Jun 2002 19:21:49
|
|
Thanks Ray and Chuck. The method works and easy to implement.
Thanks for all your help.
Kan
|
|
 |