How to access a control from another asp.net web pages code behind file.

Suppose I have two web pages. "Customers.aspx", and "Scheduling.aspx". Suppose I have a textbox control, or a checkbox control on page  "Customers.aspx". Suppose these values are named  txtCustName, chkAgeGroupBox. I can easily access these two controls from the C# code behind file Customers.aspx.cs. However I need to access these control values from the code behind file "Scheduling.aspx.cs". How can I do that?
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
Well, I am not sure why would want to do it, if you explain a bit why part I can tell you how part
EdwardCommented:
I think this is cross page posting.
https://msdn.microsoft.com/en-us/library/ms178139.aspx

My pages has a Master page.

Html Main.aspx
      <script type="text/javascript">
          function showReadMessage(inURL) {
	        popMessage = window.open(inURL, "ReadMessageWindow", "location=YES, status=NO, scrollbars=NO, resizable, directories=NO, height=650, width=500");
        	popMessage.document.title = "";
	        popMessage.focus();
	    }
    </script>

 <asp:TextBox ID="txtCustomerID" runat="server" Width="78px" AutoPostBack="True" 
        ontextchanged="txtCustomerID_TextChanged"></asp:TextBox>

<asp:Button ID="btnLookup" runat="server" Text="Look Up"  OnClientClick="javascript:showReadMessage('LookupCustomer.aspx')"  PostBackUrl="~/Web/ISO/CutterGrind/NewCutterGrind.aspx" />

Open in new window


Main.aspx.cs
    protected void txtCustomerID_TextChanged(object sender, EventArgs e)
    {

        string SQLCmd = "Select [name] from address where id = @CustomerID";
        ArrayList mySqlParameters = new ArrayList();
        mySqlParameters.Add(new SqlParameter("@CustomerId", txtCustomerID.Text.Trim()));

        clsCommonCode myCC = new clsCommonCode();
        string strCName = myCC.GetSingleValue(SQLCmd, mySqlParameters, Global.strP21Connection);

        if (strCName.Trim() == "")
            txtCustomerName.Text = "Customer Not Found";
        else
            txtCustomerName.Text = strCName;
        
    }

Open in new window




LookupCustomer.aspx (page 2) (its just a gridview)
<%@ PreviousPageType VirtualPath="~/Web/ISO/CutterGrind/NewCutterGrind.aspx" %>

Open in new window



LookupCustomer.aspx.cs
  protected void gvSearchResults_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strValue = gvSearchResults.SelectedValue.ToString();

        if (PreviousPage != null)
        {
            Control placeHolder = PreviousPage.Controls[0].FindControl("MainContent");
            TextBox SourceTextBox = (TextBox)placeHolder.FindControl("txtCustomerID");

            if (SourceTextBox != null)
                SourceTextBox.Text = strValue;

        }


    }

Open in new window



Basically, On Main.aspx there is a CustomerID textbox and a Lookup Button.  They click the button to open via javascript a popup window that sets the asp Button postbackUrl.

on the LookupCustomer.aspx its just a Gridview that pulls a list of customers from a database.  They click on the customer they want.   The gridview  selectedIndexChanged event code behind checks Page.PreviousPage (It is set on the LookupCustomer.aspx @ PreviousPageType) for a null.  If not null its Finds the Textbox on the PreviousPage then sets the value on the PreviousPage.

The important thing is to set the PreviousPage.

https://msdn.microsoft.com/en-us/library/system.web.ui.page.previouspage.aspx

I hopes this helps you out.
brgdotnetcontractorAuthor Commented:
My two pages are pretty much independent of each other. They do not depend on each other. However I need to grab the controls from another page, and get the value stored in that control. I am not using Master pages or User controls. Just simple Asp.net controls.
dejaanbuCommented:
if you are not transferring between those two pages, can you store the  txtCustName, chkAgeGroupBox values in session and access it ?.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EdwardCommented:
What I wrote has nothing to do with Master pages/ User controls.  I also use normal ASP.net controls.

So the data on one page is not dependent on another page.  not like my Customer Lookup?

So if they goto Scheduling.aspx  and bypassing Customers.aspx, then you don't have the  txtCustName and chkAgeGroupBox controls filled in.  

Let me know if I'm on the wrong path.    Customer.aspx you fill in the customer info, CustName/AgeGroup.   Then click on Scheduling to schedule something but you need the CustName/AgeGroup for that information.    

Options:  
1) Do the PreviousPage stuff.
2) Assign the CustName/AgeGroup to Session Variables.
   
   To put. 
     Session["CustName"] = txtCustName.Text; 
    Session["AgeGroup"] = chkAgeGroupBox.Checked;

 to retrieve.
    strCustName = Session["CustName"] .ToString();
    boolAgeGroup = (Bool)Session["CustName"] .ToString();

    

Open in new window


3) Save the Customer in DB,  Pass Customer Name to Scheduling.aspx via QueryString.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.