ASP. NET cross-page value passing skills

Source: Internet
Author: User
1. Use QueryString variable
QueryString is a non? Lt; why? It can display the transmitted value in the address bar of the browser. this method can be used to transmit one or more values with low security requirements or simple structure. however, this method cannot be used to pass arrays or objects. the following is an example:
A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
String s_url;
S_url = "B. aspx? Name = "+ Label1.Text;
Response. Redirect (s_url );
}
B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
Label2.Text = Request. QueryString ["name"];
}
2. Use the Application object variable
The scope of the Application object is global, that is, it is valid for all users. The common methods are Lock and UnLock.
A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
Application ["name"] = Label1.Text;
Server. Transfer ("B. aspx ");
}
B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
String name;
Application. Lock ();
Name = Application ["name"]. ToString ();
Application. UnLock ();
}
3. Use Session Variables
Presumably, this is definitely the most widely used? Lt; qomei? Its operation is similar to the Application, which acts on individual users. Therefore, excessive storage will lead to the consumption of server memory resources? ?
A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
Session ["name"] = Label. Text;
}
B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
String name;
Name = Session ["name"]. ToString ();
}
4. Use Cookie object variables
This is also a common method. Like Session, it is for every user, but there is a fundamental difference, that is, cookies are stored on the client, the session is stored on the server. in addition, the use of cookies should be combined with asp. NET built-in object Request to use.
A. aspx C # code
Private void button#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 ");
}
B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
String name;
Name = Request. Cookie ["name"]. Value. ToString ();
}
5. Use the Server. Transfer Method
This can be said to be the method used for face object development. It uses Server. the Transfer method directs the process from the current page to another page, and the new page uses the response stream of the previous page. Therefore, this method is completely object-like and concise and effective.
A. aspx C # code
Public
String Name
{
Get
{
Return Label1.Text;
}
}

Private void button#click (object sender, System. EventArgs e)
{
Server. Transfer ("B. aspx ");
}
B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
A newWeb;
// Instance a form
NewWeb = (source) Context. Handler;

String name;
Name = newWeb. Name;

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.