在ascx用户控件中的事件处理

3

如何在用户控件和父控件/页面之间传递事件的最佳实践?我想做类似于这样的事情:

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);
    }

问题是什么是最佳实践?

3个回答

6
您需要在控件上创建一个事件,该事件被父级订阅。有关示例,请参见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>

用户控件的代码如下所示。
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

}

用户控件指定了一个公共事件(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>

在页面的后台代码中。
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>");         
   }
}

注意,父页面只需在InitializeComponent方法中添加事件处理程序。当我们收到事件时,我们将再次使用Reponse.Write来跟踪执行流程。

一个警告:如果任何时候事件神秘地停止工作,请检查InitializeComponent方法,以确保设计器没有删除添加事件处理程序的任何代码。


2

1) 在用户控件中声明一个公共事件。

2) 在用户控件内适当的位置发出RaiseEvent。

3) 在父页面的Init事件中,使用AddHandler将control.event分配给您想要使用的处理过程。

就这么简单!


0

我在OdeToCode上找到了与@lordscarlet在他的被接受的解决方案中链接的相同解决方案。问题是我需要使用VB而不是C#的解决方案。它不能完美转换。具体来说,在OnBubbleClick中检查事件处理程序是否为null在VB中无法工作,因为编译器认为我正在尝试调用事件,并且显示一个错误,指出“...不能直接调用。请使用“RaiseEvent”语句来引发事件。”因此,这是一个针对OdeToCode解决方案的VB翻译,使用名为CountryDropDownList的控件。

首先,让我们创建一个带有附加下拉列表的用户控件。

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="CountryDropDownList.ascx.vb" Inherits="CountryDropDownList" %>
<asp:DropDownList runat="server" ID="ddlCountryList" OnSelectedIndexChanged="ddlCountryList_SelectedIndexChanged" AutoPostBack="true">
    <asp:ListItem Value=""></asp:ListItem>
    <asp:ListItem value="US">United States</asp:ListItem>
    <asp:ListItem value="AF">Afghanistan</asp:ListItem>
    <asp:ListItem value="AL">Albania</asp:ListItem>
</asp:DropDownList>

用户控件的后台代码如下所示。
Public Class CountryDropDownList
    Inherits System.Web.UI.UserControl
    Public Event SelectedCountryChanged As EventHandler

    Protected Sub ddlCountryList_SelectedIndexChanged(sender As Object, e As EventArgs)
        ' bubble the event up to the parent
        RaiseEvent SelectedCountryChanged(Me, e)
    End Sub
End Class

一个ASPX页面现在可以使用用户控件。
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="UpdateProfile.aspx.vb" Inherits="UpdateProfile" MaintainScrollPositionOnPostback="true" %>
<%@ Register Src="~/UserControls/CountryDropDownList.ascx" TagPrefix="SO" TagName="ucCountryDropDownList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>WebForm1</title>
    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <SO:ucCountryDropDownList id="ddlCountry" runat="server" />
        </form>
    </body>
</HTML>

在页面的代码后台:
Protected Sub OnSelectedCountryChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCountry.SelectedCountryChanged
    ' add your code here
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接