繁体   English   中英

为什么我无法通过页面标记为用户控件的事件添加事件处理程序

[英]why i can't add event handlers for events of my usercontrol though page markup

我有一个具有公共事件的用户控件。 当我在网页中使用此用户控件并通过页面标记将事件设置为代码中事件处理程序的事件时,不会设置用户控件的事件,即使在第一个页面加载时也始终为null。 这是我的usercontrol的一部分,其中有一个detailsview,我正在尝试为以下事件设置事件:

[Serializable(), PersistChildren(false)]
public partial class UserControl_UCDetailsView : System.Web.UI.UserControl
{
    //here is an event for exposing detailsview1 event to the outside code
    public event DetailsViewUpdateEventHandler OnItemUpdating;
    protected void detailsview1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        //here i want it not to be null but it is. by using break points i found out 
        //this is always null and never actually get set while i've set it in markup!
        if (this.OnItemUpdating != null)
            this.OnItemUpdating(sender, e);
    }
}

这是我的用户控件标记的一部分:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UCDetailsView.ascx.cs"
    Inherits="UserControl_UCDetailsView" %>
<asp:DetailsView ID="detailsview1" runat="server" OnItemUpdating="detailsview1_ItemUpdating">
</asp:DetailsView>

这就是我在页面标记中设置事件的方式:

<UC:DetailsView ID="ucDetails" runat="server" OnItemUpdating="ucDetails_ItemUpdating">
</UC:DetailsView>

在ucDetails_ItemUpdaing中,我有一个真正的更新逻辑,例如设置字段等。所以,如果我在页面加载时设置事件(如以下所示),为什么会出现问题呢? 我想念什么吗? 也许是一个属性?

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ucDetails.OnItemUpdating += new DetailsViewUpdateEventHandler(ucDetails_ItemUpdating);
    }
}

公开添加和删除为公共属性。

注意: 我刚刚在SO上看到了这个答案

public event DetailsViewUpdateEventHandler ItemUpdating; 

public event DetailsViewUpdateEventHandler OnItemUpdating
{ 
   add { this.ItemUpdating += value;} 
   remove { this.ItemUpdating -= value;} 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM