asp.net cross-page Transfer value method _ Practical Skills

Source: Internet
Author: User
1. Using QueryString variables
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 is an example:
A.aspx's C # code
Copy Code code as follows:

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
Copy Code code as follows:

private void Page_Load (object sender, EventArgs e)
{
Label2.Text = request.querystring["name"];
}

2. 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
Copy Code code as follows:

private void Button1_Click (object sender, System.EventArgs e)
{
application["name"] = Label1.Text;
Server.Transfer ("b.aspx");
}

C # code in b.aspx
Copy Code code as follows:

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 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
Copy Code code as follows:

private void Button1_Click (object sender, System.EventArgs e)
{
session["name"] = Label.text;
}

C # code in b.aspx
Copy Code code as follows:

private void Page_Load (object sender, EventArgs e)
{
String name;
Name = session["Name"]. ToString ();
}

4. 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
Copy Code code as follows:

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
Copy Code code as follows:

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 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
Copy Code code as follows:

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
Copy Code code as follows:

private void Page_Load (object sender, EventArgs e)
{
a newweb; Instance a form
Newweb = (source) Context.Handler;
String name;
name = Newweb.name;
}

6.post method
Copy Code code as follows:

<form action= "default.aspx" "method=" Post "enctype=" application/ X-www-form-urlencoded "Name=" Form1 id= "Form1" >
<label>xm
<input name= "XM" type= "text" id= "XM" Val Ue= "Xiaoqi" style= "width:149px"/>
</label>
<label><br/>
XB
<input name= "XB" type= "text" id= "XB" value= "nan" style= "width:154px"/>
</label>
<p>
<input type= "Submit" name= "button" id= "button" value= "Send"/>
</p>
</form>
//Get client parameter (POST) 2
th Is. Response.Write ("<br/>" + this. Request.form["XM"]);
this. Response.Write ("<br/>" + this. request.form["XB"]);
this. Response.Write ("<br/> Request Type:" + this. Request.requesttype);
this. Response.Write ("<br/> read post success from client!") ");

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.