Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:34:22+00:00 2026-06-08T22:34:22+00:00

I have searched extensively on this, but cannot find the solution to my problem.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:34:24+00:00Added an answer on June 8, 2026 at 10:34 pm

    I’ve been struggling with events for quite a while as well. Nowadays I always create them this way ’cause it’s the only way I know it works. Haven’t tested it with your code but here it goes anyway:

    public partial class ucBuyerList : System.Web.UI.UserControl
    {
        public delegate void BuyerSelectedEventHandler(object sender, EventArgs e);
    
        public event BuyerSelectedEventHandler BuyerSelected;
    
        public string Name;
        public string AUID;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            //Select the first buyer in the list when the user control loads
    
            if (!IsPostBack)
            {
                lbBuyerList.SelectedIndex = 0;
            }
        }
    
        private void OnBuyerSelected(EventArgs e)
        {
            BuyerSelectedEventHandler handler = BuyerSelected;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }
    
        protected void lbBuyerList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Name = lbBuyerList.SelectedItem.Text;
            AUID = lbBuyerList.SelectedItem.Value;
            OnBuyerSelected(e);
        }
    }
    

    In the parent page you can just call your function the same way you’re doing it already.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched this site extensively but cannot find a solution. Here is the
I have searched for a general solution to this but only find answers to
(have searched, but not been able to find a simple solution to this one
I have searched all morning and yesterday afternoon and still cannot find an solution
I have searched extensively and cannot for the life of me find any information
I have searched and searched for this but I think my terminology isn't correct
I have searched & searched but I seem unable to find a way to
I have searched quite extensively throught stackoverflow but unfortunately found no conclusive answer to
I have been trying to figure this out all day. I've searched SO and
I have searched for this online, but am still a bit confused (as I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.