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

  • Home
  • SEARCH
  • 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 6763433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:28:31+00:00 2026-05-26T14:28:31+00:00

I have problem passing a variable from a main page containing a user control

  • 0

I have problem passing a variable from a main page containing a user control to the user control itself. Although the passed variable is available generally in the code-behind of the user control the page_load event can’t seem to read it.

My code –

In the main page code-behind:

protected void FindCCFsButton_Click(object sender, EventArgs e)
    {
        if (CustomerDropDown.SelectedIndex != 0)
        { SearchUcCCFList.SetCustID(CustomerDropDown.SelectedValue); }
    }

(SearchUcCCFList is the instance of the user control in the main aspx page).
In the user control code behind:

public partial class ucCCFList : System.Web.UI.UserControl
{
    public string srchCust { get; set; }

    public void SetCustID(string custID)
    {
        srchCust = custID;
        testCustLabel.Text = GetCustID(); //this works
    }

    public string GetCustID()
    {
        return srchCust;
    }

protected void Page_Load(object sender, EventArgs e)
    {
        CCFGridView.DataSource = DAL.SearchCCFs(custID : GetCustID());  //doesn't work
        CCFGridView.DataBind();
        test2CustLabel.Text = GetCustID();  //doesn't work
    }

In the Page_Load event GetCustId() doesn’t return anything (so the records aren’t filtered and all get returned) although it can be read in the methods outside the Page_Load.

I’m probably making a beginners error but any help would be appreciated.

Edit – following Alan’s suggestion in the comments I stepped through the page loading sequence & it appears that the user control’s Page_Load event is running BEFORE the code in the main page’s button click so the variable is not yet available. The sequence after clicking the button is:

  1. User control Page_Load runs
  2. Code in button event on main page
  3. Other code (outside Page_Load) in user control runs hence variable is available here.

This seems a bit weird, is there another way to pass the variable into the user controls Page_Load?

  • 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-05-26T14:28:32+00:00Added an answer on May 26, 2026 at 2:28 pm

    In this case, your click handling even on the main page is called after the user control page load call. Your variable is being set, but not until after your data binding in the user control.

    Either switch the user control to declarative binding which will handle calling methods in the correct order for you. Or the easier fix in this case is to change the user control data binding from Page_Load to Page_PreRender, which is called later in the life cycle, after the main page click handling call.

    protected void Page_PreRender(object sender, EventArgs e)
    {
        CCFGridView.DataSource = DAL.SearchCCFs(custID : GetCustID());  // will work now
        CCFGridView.DataBind();
        test2CustLabel.Text = GetCustID();  // will work now
    }
    

    For a more thorough answer, read up on the ASP.NET page life cycle including the interaction with user controls’ life cycle.

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

Sidebar

Related Questions

I have a problem with declaring a type as passed from parameter type variable.
I have problem on passing jquery variable on codeigniter controller. actually i want to
----> I have datatable which is passing to another page in session variable. ---->
Here's my problem: I have a big main.c file containing 40 or so global
I'm experimenting with WCF Services, and have come across a problem with passing Interfaces.
My problem is similar to Django Passing Custom Form Parameters to Formset Ive have
I am having a problem with views. I have a view and am passing
Ok now i have another problem i want to send one or more variable
First, I'll lay out the structure: The main report is a table variable from
I have a problem displaying a View Controller when calling a segue from code

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.