ASP. NET Global Variables and page-by-pass method

Source: Internet
Author: User
Tags server memory

Http://www.cnblogs.com/dgjack/archive/2011/05/28/2060913.html

1. Using the QueryString variable querystring is a very simple way to transmit values that can be displayed in the browser's address bar.

You can use this method if you are passing one or more security requirements that are not high or have a simple structure. However, you cannot use this method for passing arrays or objects.

Here is an example:

C # code for a.aspx

private void Button1_Click (object sender, System.EventArgs e)

{

String S_url;

S_url = "B.aspx?name=" + Label1.Text;

Response.Redirect (S_url);

}

C # code in b.aspx

private void Page_Load (object sender, EventArgs e)

{

Label2.Text = request.querystring["name"];

}

2. Using the Application object variable

The Application object is scoped to the whole global, which means it is valid for all users. Its common method is to use lock and unlock.

C # code for a.aspx

private void Button1_Click (object sender, System.EventArgs e)

{

application["name"] = Label1.Text;

Server.Transfer ("b.aspx");

}

C # code in b.aspx

private void Page_Load (object sender, EventArgs e)

{

String name; Application.Lock ();

Name = application["Name"]. ToString ();

Application.UnLock ();

}

3. Use the session variable

Presumably this is the most common usage of everyone, its operation is similar to application, acting on the user's personal, so, excessive storage will lead to the depletion of server memory resources.

C # code for a.aspx

private void Button1_Click (object sender, System.EventArgs e)

{

session["name"] = Label.text;

}

C # code in b.aspx

private void Page_Load (object sender, EventArgs e)

{

String name;

Name = session["Name"]. ToString ();

}

4. Using Cookie Object variables

This is also a common use of the method, as in the session, what it is for each user, but there is an essential difference, that is, the cookie is stored in the client, and the session is stored on the server side.

And the use of cookies should be used in conjunction with the ASP.

C # code for a.aspx

private void Button1_Click (object sender, System.EventArgs e)

{HttpCookie cookie_name = new HttpCookie ("name");

Cookie_name. Value = Label1.Text;

Reponse.appendcookie (Cookie_name); Server.Transfer ("b.aspx");

}

C # code in b.aspx

private void Page_Load (object sender, EventArgs e)

{

String name;

Name = request.cookie["Name"]. Value.tostring ();

}

5. Using the Server.Transfer method

This can be said to be the method used by the development of the face-like object, which uses the Server.Transfer method to direct the process from the current page to another page, the new page uses the reply stream of the previous page,

So this method is completely object-like, concise and effective.

C # code for a.aspx

public string Name

{

get{return Label1.Text;}

}

private void Button1_Click (object sender, System.EventArgs e)

{

Server.Transfer ("b.aspx");

}

C # code in b.aspx

private void Page_Load (object sender, EventArgs e)

{

a newweb; Instance a form

Newweb = (source) Context.Handler;

String name;

name = Newweb.name;

}

ASP. NET Global Variables and page-by-pass method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.