Event Handler is Always null

11,804

It could be that Page_Load is too late in the page lifecycle to be using LoadControl and subscribing to the event. What happens if you move that code to the Page_Init method?

Share:
11,804
Admin
Author by

Admin

Updated on August 11, 2022

Comments

  • Admin
    Admin over 1 year

    I have searched extensively on this, but cannot find the solution to my problem. I am trying to call a function in the code behind of a page from a user control on that page.

    I have a web application that uses a master page. I am adding a user control that I wrote to one of the content pages. I added the user control to the aspx page by dragging and dropping it from the toolbox. I am able to see the user control from the code behind, but I cannot access the public functions. To fix that problem, I created an object of the user control in the code behind and used the LoadControl function. All of that seems to work fine.

    The problem I am having is when I am trying to hook the into the EventHandler from the aspx page to the user control. Everything compiles and runs just fine, but I am not seeing anything happen on the page. I think the issue is that the EventHandler is always null.

    User Control Code

    public partial class ucBuyerList : System.Web.UI.UserControl
    {
        public delegate void BuyerSelectedEventHandler(object sender, EventArgs e);
        public event BuyerSelectedEventHandler BuyerSelected;
    
        private string name = "";
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    
        private string auid = "";
        public string AUID
        {
            get { return auid; }
            set { auid = value; }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        private void OnBuyerSelected(EventArgs e)
        {
            if (BuyerSelected != null)
            {
                BuyerSelected(this, new EventArgs());
            }
        }
    
        protected void lbBuyerList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetNameAndAUID();
            OnBuyerSelected(e);
        }
    
        private void SetNameAndAUID()
        {
            name = lbBuyerList.SelectedItem.Text;
            auid = lbBuyerList.SelectedItem.Value;
        }
    }
    

    Parent Page Code

        public partial class frmBuyerInformation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.changePageTitle("Buyer Information");
            buyerList.BuyerSelected += new ucBuyerList.BuyerSelectedEventHandler(buyerListControl_BuyerSelected);
        }
    
        void buyerListControl_BuyerSelected(object sender, EventArgs e)
        {
            DisplayBuyerInformation();
        }
    
        public void DisplayBuyerInformation()
        {
            tbName.Text = buyerList.Name;
            tbAUID.Text = buyerList.AUID;
        }
    }
    

    Can anyone see what I am doing wrong?

    EDIT: This issue has been resolved. The code snippits above are now functional. If anyone runs into the issue I had, you can model the code above. Make sure that AutoEventWireup="true" in both the aspx and ascx pages. Thank you June Paik for your solution. Thank you Diego De Vita for your input as well.