Asp. NET page Pass Value method

Source: Internet
Author: User

1. Use querystring, such as ...? id=1; Response. Redirect () ....

2. Use Session Variables

3. Use of Server.Transfer

4. Use a cookie to pass the value

5.Application

6.<input type= "hidden" ></input>

7. The database

1. Response.Redirect (or Query String method, url method)

Response.Redirect ("webform5.aspx");

First let's look at the Response.Redirect method, which should be the simplest, when we click on the Response.Redirect button to trigger the above line of code. Here's a tip to share: there are times when we put the above code in try{...} catch (), that is, when we catch the exception and pass the exception to another page. If we try to do this, we may get a "system.threading" exception, because we jump to another page, and the original page thread is still running. This can be done to solve this problem:

Response.Redirect ("Webform5.aspx", false);

This means telling the compiler to "webform5.aspx" while the "false" argument tells the compiler not to stop the activity on the original page, which we can see in the System.Threading class.

In the following code, "Txtname" is the name of the text box for the value to be passed, which follows the "?" The following "Name" is the flag of a temporary response variable that hosts the Txtname value.

private void Button1_Click (object sender, System.EventArgs e)
{
    //Value sent using HttpResponse
    Response.Redirect ("Webform5.aspx?") Name= "+txtname.text);
}

OK, to this location, we have completed the response value. But how do we receive the value in "Webform5.aspx"? Don't worry, we'll write the following code in the "Webform5.aspx" Page_Load event. First, we want to confirm that the value is not "null", if it is not "null", we can use the label to display the value.

Note: When we use Response.Redirect to pass variables, all variables can be seen in the URL of the browser, we can not use this method to pass important confidential data, such as credit card number.

if (request.querystring["Name"]!= null)

Label3.text = request.querystring["Name"];

2. Cookies Way

Next is cookies, cookies are created by the server, but stored on the client, and when we click on the "Cookies" button, we run the following code:

HttpCookie cName = new HttpCookie ("Name");

Cname.value = txtName.Text;

RESPONSE.COOKIES.ADD (CName);

Response.Redirect ("webform5.aspx");

First, we created a cookie instance called "CName", and since a cookie instance can hold multiple values, we need to tell the compiler that the cookie will hold the "Name" value and assign the txtName.Text value to it and add it to the output stream. and use Response.Redirect output to another page.

Then let's see how to remove the value from the cookie in the target page:

if (request.cookies["Name"]!= null)

Label3.text = request.cookies["Name". Value;

Obviously, the steps are very similar to the previous one, just using request.cookies instead of request.querystring.

Note: Some browsers do not support cookies.

3. Session variable

Next we look at the session variables that are maintained on the server side. The session is created when the user makes the first request to the server, and terminates when the user closes the browser or the exception occurs (in fact, it has expired). The following code is an example of using session to pass values. We can see that the session creates the "Name" key for the user and assigns the value of the textbox to it.

Session creation
    
session["Name" = txtName.Text; 
    
Response.Redirect ("webform5.aspx");
    
    
    
The following code shows how to take the value//code from the session to the
    
other page
    
    
    
if (session["name"]!= null) 
    
    Label3.text = session["Name"]. ToString ();

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.