开发者

Firing events in UserControls (ASP.NET) - the event object is null?

I have a problem with the Event object being null when I want to fire the event. There are other threads here and on the net about this:

  • asp.net: send an object's event from a UserControl to its Parent

  • http://objectmix.com/csharp/120067-event-object-null-when-event-fired-webusercontrol-asp-net-ajax.html

However, I have tried according to what is recommended in those post, but the event object is null. Here is the code:

In my Page who is to listen to the Event:

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

The Page_Load above is execute开发者_如何学Cd well before the EVent is fired (below).

The code in the UserControl containing the 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);
        }
}

Is it ViewState or something like that, that I am missing? Why is CustomerSelected always null?


Your eventhandler should be subscribed in every page load and not just the !postback

protected void Page_Load(object sender, EventArgs e)
{

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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜