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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…