在UserControls(ASP.NET)中触发事件 - 事件对象为空?

时间:2011-05-13 12:40:58

标签: asp.net events viewstate

当我想要触发事件时,我有一个问题,即Event对象为null。这里和网上还有其他线索:

但是,我已根据这些帖子中推荐的内容进行了尝试,但事件对象为null。这是代码:

在我的页面中听谁的事件:

protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
     {
          fieldCustomer1.CustomerSelected += new UserControls.Field_Customer.uintDelegate(fieldCustomer1_CustomerSelected);
     }
}

上面的Page_Load在触发EVent之前执行(下图)。

UserControl中包含Event:

的代码
public partial class Field_Customer : System.Web.UI.UserControl
{
        public delegate void uintDelegate(uint id);
        public event uintDelegate CustomerSelected;

        // ... yada yada yada code code code

        [DirectMethod]  // ext.net stuff
        public void FireCustomerSelected()
        {
            if (CustomerSelected != null) // the CustomerSelected is always null
                CustomerSelected(_CustomerId);
        }
}

ViewState或类似的东西,我错过了吗?为什么CustomerSelected始终为空?

1 个答案:

答案 0 :(得分:4)

您的eventhandler应该在每个页面加载中订阅,而不仅仅是!postback

protected void Page_Load(object sender, EventArgs e)
{

          fieldCustomer1.CustomerSelected += new UserControls.Field_Customer.uintDelegate(fieldCustomer1_CustomerSelected);

}