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

前端 未结 2 1320
梦如初夏
梦如初夏 2020-12-20 07:31

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

using System;
         


        
相关标签:
2条回答
  • 2020-12-20 08:05

    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.

    0 讨论(0)
  • 2020-12-20 08:13

    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.

    0 讨论(0)
提交回复
热议问题