按照这个例子,我试图在用户控制级别引发一个事件,然后将它冒泡到后面的 aspx 代码。但是,似乎公共事件从未附加(或始终为空),因此它永远不会冒泡。我的代码如下:
// WebUserControl1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="testEventeventBubbling.WebUserControl1" %>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
// WebUserControl1.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("WebUserControl1 :: Page_Load <BR>");
}
protected void Button1_Click(object sender, 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)
{
// BubbleClick is always null at this point
if (BubbleClick != null)
{
BubbleClick(this, e);
}
}
// Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="testEventeventBubbling._Default" %>
<%@ Register TagPrefix="wuc" TagName="BubbleControl" Src="WebUserControl1.ascx" %>
...
<wuc:BubbleControl id="BubbleControl1" runat="server" />
// Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
BubbleControl1.BubbleClick += new EventHandler(BubbleControl1_BubbleClick);
}
void BubbleControl1_BubbleClick(object sender, EventArgs e)
{
throw new NotImplementedException();
}
我很确定我在正确的轨道上,但不知道我是否错过了任何明显或愚蠢的事情。任何人都可以对此有所了解吗?
提前致谢,