How do I access a variable from the code behind of another page?

I have an index.aspx (index.aspx.cs) which will include the body.aspx (body.aspx.cs) using Server.execute("body.aspx");

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;

public partial class index : System.Web.UI.Page
{
    public string text1 = "abc";

    protected void Page_Load(object sender, EventArgs e)
    {
        

    }

}

In index.aspx.cs, there is a variable text1 which I want to use in body.aspx.cs, how to do that?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I think you’re thinking of ASP.NET the wrong way. I guess you started as a Windows Developer.

ASP.NET Forms are not like Windows Forms.

You have to understand that the ASP.NET page only lives until the request is being served. And then it “dies”.

You cannot pass variables from/to pages the same way you do with Windows Forms.

If you want to access contents from another page. Then this page MUST store that piece of information inside a SESSION object and then you access that session object from a different page and get the value that you want.

Let me give you an example:

Page 1:

public string text1 = "abc";

    protected void Page_Load(object sender, EventArgs e)
    {
          Session["FirstName"] = text1;
    }

Page 2:

protected void Page_Load(object sender, EventArgs e)
{
    string text1;          
    text1 = Session["FirstName"].ToString();
}

That’s how you pass values between pages that are not linked together.

Also, you can pass values using by modifying the Query string (add variables to the URL).

Example:

Page 1: (button click event)

private void btnSubmit_Click(object sender, System.EventArgs e)
{
    Response.Redirect("Webform2.aspx?Name=" +
    this.txtName.Text + "&LastName=" +
    this.txtLastName.Text);
}

Page 2:

private void Page_Load(object sender, System.EventArgs e)
{
   this.txtBox1.Text = Request.QueryString["Name"];
   this.txtBox2.Text = Request.QueryString["LastName"];
}

this is how you should pass variables between pages

Also, if you want a value to be shared between ALL visitors of your website. Then you should consider using Application instead of Session.

Method 2

If you mark the variable as static, it ceases to become a property of a particular instance of the page and becomes a property of the type of page.

You can then access it as index.text1 from anywhere that can see the index class.

However, this means that the value is then shared between every instance of this page: if it is changed by an instance of the page (or by any other class that can now see it), subsequent page loads will reflect the changed value.

If you don’t want this – if this variable should be different between each instance of the page – then what you want isn’t possible. ASP.NET pages don’t exist other than while they’re being generated by the server, so there is no page for you to get this value from.

If this value is never meant to change, mark it as const and you don’t have to worry about things changing it.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x