将事件从usercontrol传递到父控件/页面的最佳实践是什么?我想做类似这样的事情:
MyPage.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceholder" runat="server">
<uc1:MyUserControl ID="MyUserControl1" runat="server"
OnSomeEvent="MyUserControl_OnSomeEvent" />
MyUserControl.ascx.cs:
public partial class MyUserControl: UserControl
{
public event EventHandler SomeEvent;
....
private void OnSomething()
{
if (SomeEvent!= null)
SomeEvent(this, EventArgs.Empty);
}
Run Code Online (Sandbox Code Playgroud)
问题是什么是最佳做法?
您可能希望在父级中订阅的控件上创建一个事件.有关示例,请参阅OdeToCode.
这是长寿的文章:
某些用户控件完全是自包含的,例如,显示当前股票报价的用户控件不需要与页面上的任何其他内容交互.其他用户控件将包含要回发的按钮.虽然可以从包含页面订阅按钮单击事件,但这样做会破坏一些面向对象的封装规则.更好的想法是在用户控件中发布一个事件,以允许任何感兴趣的各方处理该事件.
这种技术通常被称为"事件冒泡",因为事件可以继续穿过层,从底部开始(用户控制)并且可能到达顶层(页面),就像气泡向上移动香槟杯.
对于初学者,让我们创建一个带有按钮的用户控件.
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="WebUserControl1.ascx.cs"
Inherits="aspnet.eventbubble.WebUserControl1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"
%>
<asp:Panel id="Panel1" runat="server" Width="128px" Height="96px">
WebUserControl1
<asp:Button id="Button1" Text="Button" runat="server"/>
</asp:Panel>
Run Code Online (Sandbox Code Playgroud)
用户控件的代码如下所示.
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Panel Panel1;
private void Page_Load(object sender, System.EventArgs e)
{
Response.Write("WebUserControl1 :: Page_Load <BR>");
}
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("WebUserControl1 :: Begin Button1_Click <BR>");
OnBubbleClick(e);
Response.Write("WebUserControl1 :: End Button1_Click <BR>");
}
public event EventHandler BubbleClick;
protected void OnBubbleClick(EventArgs e)
{
if(BubbleClick != null)
{
BubbleClick(this, e);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
用户控件指定声明委托的公共事件(BubbleClick).任何对BubbleClick事件感兴趣的人都可以添加一个EventHandler方法,以便在事件触发时执行 - 就像用户控件在Button触发Click事件时添加一个EventHandler一样.
在OnBubbleClick事件中,我们首先检查是否有人附加到事件(BubbleClick!= null),然后我们可以通过调用BubbleClick调用所有事件处理方法,通过EventArgs参数并设置用户控件(this)作为事件发件人.请注意,我们还使用Response.Write来跟踪执行流程.
ASPX页面现在可以使用户控件工作.
<%@ Register TagPrefix="ksa"
TagName="BubbleControl"
Src="WebUserControl1.ascx"
%>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="aspnet.eventbubble.WebForm1"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<ksa:BubbleControl id="BubbleControl" runat="server" />
</form>
</body>
</HTML>
Run Code Online (Sandbox Code Playgroud)
在页面背后的代码中.
public class WebForm1 : System.Web.UI.Page
{
protected WebUserControl1 BubbleControl;
private void Page_Load(object sender, System.EventArgs e)
{
Response.Write("WebForm1 :: Page_Load <BR>");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
BubbleControl.BubbleClick += new EventHandler(WebForm1_BubbleClick);
}
#endregion
private void WebForm1_BubbleClick(object sender, EventArgs e)
{
Response.Write("WebForm1 :: WebForm1_BubbleClick from " +
sender.GetType().ToString() + "<BR>");
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,父页面只需要在InitializeComponent方法期间添加事件处理程序.当我们收到事件时,我们将再次使用Reponse.Write来跟踪执行流程.
一句警告:如果在任何时候事件神秘地停止工作,请检查InitializeComponent方法以确保设计者没有删除任何添加事件处理程序的代码.
归档时间: |
|
查看次数: |
12664 次 |
最近记录: |