asp.net page jump values in several ways __.net

Source: Internet
Author: User
Tags server memory
protected void Button1_Click (object sender, EventArgs e) {//Use QueryString to transmit value//RESPONSE.R

            Edirect ("b.aspx?username=" + TextBox1.Text.Trim () + "&&pwd=" + TextBox2.Text.Trim ());
            Use session Pass value//session["username"] = TextBox1.Text.Trim ();
            session["pwd"] = TextBox2.Text.Trim ();

            Response.Redirect ("b.aspx");
            Use Application to//application["username"] = TextBox1.Text.Trim ();
            application["pwd"] = TextBox2.Text.Trim ();

            Response.Redirect ("b.aspx");
            Use a cookie to pass the value//httpcookie HC = new HttpCookie ("username", TextBox1.Text.Trim ());
            HttpCookie hc2 = new HttpCookie ("pwd", TextBox2.Text.Trim ());
            Response.appendcookie (hc);//Adds the created cookie to the internal cookie collection//response.appendcookie (HC2); Server.Transfer ("b.aspx"); Cookies need to use Server.Transfer to jump//use Server.Transfer to transfer values//sErver.
            The transfer method directs the process from the current page to another page, and the new page uses the response stream from the previous page, so the method is completely object-like.
 Server.Transfer ("b.aspx"); ///Use Server.Transfer//public string getusername//{//get {return TextBox1.Text. Trim ();
        }//}//public string Getpwd//{//get {return TextBox2.Text.Trim ();} //}

1. Using the QueryString variable querystring is a very simple way of passing values, and he can display the value of the transfer in the browser's address bar. You can use this method if you pass one or more values that are not of high security requirements or are simple in structure. However, this method is not available for passing arrays or objects.

Here's an example: A.aspx's C # code

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. Use Session Variables

Presumably this is the most common use of everyone, its operation is similar to application, the role of users, so, excessive storage will lead to the depletion of server memory resources.

A.aspx's C # code

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 (); 
}

3. Use Cookie object variables

This is also common use of the method, as with the session, what it is for each user, but there is an essential difference, that is, cookies are 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.net built-in object request.

A.aspx's C # code

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 (); 
}

4. Using Application Object Variables

The scope of the Application object is the entire global, which means it works for all users. Its commonly used methods are lock and unlock.

A.aspx's C # code

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 (); 
}


5. Using the Server.Transfer method

This can be said to be the object of the development of the method used, which uses the Server.Transfer method to guide the process from the current page to another page, the new page uses the previous page of the response stream, so this method is completely surface image object, concise and effective.

A.aspx's C # code

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; 



Related Article

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.