Ted
Ted

Reputation: 20184

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:

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 executed 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?

Upvotes: 0

Views: 1807

Answers (1)

FiveTools
FiveTools

Reputation: 6030

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

}

Upvotes: 4

Related Questions