我有两个 UserControl - PageDefault.aspx 中的 UserControl1.ascx 和 UserControl2.ascx:
如何使用事件冒泡从 UserControl2.ascx
调用方法(GetLabelText() in UserControl1.ascx
)?
这是我的示例代码 - 当我单击按钮时(UserControl1.ascx 中的 UserControl2Button1) - 我想从 UserControl2.ascx 调用方法 GetLabelText()
- 使用事件冒泡。
PageDefault.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageDefault.aspx.cs" Inherits="TelerikAjaxEvents.PageDefault" %>
<%@ Register TagPrefix="uc" TagName="UserControl1" Src="~/UserControl1.ascx" %>
<%@ Register TagPrefix="uc" TagName="UserControl2" Src="~/UserControl2.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Page Default</title>
</head>
<body>
<form id="form1" runat="server">
UserControl1:
<uc:UserControl1 ID="UserControl1" runat="server" />
UserControl2:
<uc:UserControl2 ID="UserControl2" runat="server" />
</form>
</body>
</html>
UserControl1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="TelerikAjaxEvents.UserControl1" %>
<asp:Label ID="UserControl1Label1" runat="server"></asp:Label>
UserControl1.ascx.cs
public partial class UserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void GetLabelText()
{
UserControl1Label1.Text = "Text is Visible";
}
}
UserControl2.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl2.ascx.cs" Inherits="TelerikAjaxEvents.UserControl2" %>
<asp:Button ID="UserControl2Button1" runat="server" Text="Send"
onclick="UserControl2Button1_Click" />
UserControl2.ascx.cs
public partial class UserControl2 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UserControl2Button1_Click(object sender, EventArgs e)
{
//Call method from UserControl1 (GetLabelText()) - Show Label text - USING BUBBLE EVENT
}
}
最佳答案
有很多方法可以解决这个问题。 Mark 的答案暗示了传统上所谓的事件冒泡,使用 System.Web.UI.Control 基本控件的内置功能部分。然而,公开您自己的事件、绑定(bind)到它并通过控制层次结构冒泡事件是一个简单的练习。有关在 ASP.NET 中使用 BubbleEvent 的更多详细信息,请阅读以下内容。请记住,这两篇 MSDN 文章都是针对 .NET 1.1 编写的
K. Scott Allen 在他的文章中很好地展示了“事件冒泡”实现的具体情况:Event Bubbling From Web User Controls in ASP.NET (C#) 。请参阅以下对示例的修改以获取灵感。
UserControl1 与 GetLabelText()
public partial class UserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void GetLabelText()
{
UserControl1Label1.Text = "Text is Visible";
}
}
UserControl2 具有公开的 BubbleClick 事件处理程序,调用者可以订阅该事件处理程序。
public partial class UserControl2 : System.Web.UI.UserControl
{
protected Button UserControl2Button1;
protected void Page_Load(object sender, EventArgs e)
{
}
public event EventHandler BubbleClick;
protected void OnBubbleClick(EventArgs e)
{
if(BubbleClick != null)
{
BubbleClick(this, e);
}
}
protected void UserControl2Button1_Click(object sender, EventArgs e)
{
// do some stuff
OnBubbleClick(e);
}
}
PageDefault 订阅 UserControl2 的 BubbleClick 事件处理程序并执行 UserControl1.GetLabelText()
public partial class PageDefault : WebPage
{
UserControl1 userControl1;
UserControl2 userControl2;
protected void Page_Load(object sender, EventArgs e)
{
UserControl2.BubbleClick += RootBubbleClickHandler;
}
protected void RootBubbleClickHandler(object sender, EventArgs e)
{
if (sender is UserControl2)
{
// subscribe UserControl1 to UserControl2 click event and do something
userControl1.GetLabelText();
}
// check for other controls with "BubbleClicks"
}
}
关于asp.net - ASP.NET 中 Web 用户控件的事件冒泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11275195/