ASP. NET page value transfer Summary (session/server. Transfer/query string/Cookie/Application)

Source: Internet
Author: User
ArticleDirectory
    • 4. Application variable

Address: http://www.codeproject.com/KB/aspnet/TransferingValues.aspx

 

 

Introduction:

 

Web ApplicationProgramIn development, passing values between pages should be the most common problem. In this article,AzamsharpThis section describes how to pass values on ASP. NET pages. The example in this article is very simple. It only contains one text box and several buttons. When a button is clicked, the strings in the text box will be passed to another page in different ways.

 

1. response. Redirect (or query string or URL)

 

Response. Redirect ("Webform5.aspx");

 

First, let's take a look at the response. Redirect method. This should be the simplest. When we click the response. Redirect button, the line above will be triggered.CodeYou can. Here is a tips: Sometimes we will put the above Code in try {...} catch (), that is, when we capture an exception and pass the exception to another web page. If we try to do this, we may get a "system. Threading" exception, because we jumped to another page, and the original page thread is still running. You can solve this problem as follows:

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

This means that the compiler is told to go to "webform5.aspx", and the "false" parameter tells the compiler not to stop the activity on the original page. For details, refer to the system. Threading class.

In the following code, "txtname" is the name of the text box for the value to be passed in. The following "name" is a flag of the temporary response variable, which carries the txtname value.

 

Private   Void Button#click ( Object Sender, system. eventargs E)
{
// Value sent using httpresponse
Response. Redirect ( " Webform5.aspx? Name = " + Txtname. Text );
}

 

OK. At this point, the value of response has been passed. However, in "webform5.aspx", how do we receive incoming values? Don't worry. We will write the following code in the page_load event of "webform5.aspx. First, we need to confirm that the incoming value is not "null". If it is not "null", we can use label to display the value.

Note: When response. Redirect is used to transmit variables, all variables can be seen in the URL of the browser. We cannot use this method to transmit important confidential data, such as credit card numbers.

If (Request. querystring [ " Name " ] ! =   Null )
Label3.text = Request. querystring [ " Name " ];

 

2. Cookies

 

Next, cookies are created by the server, but saved on the client. When we click "cookies", 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 named "cname". Since one cookie instance can save multiple values, we need to tell the compiler that this cookie will save the "name" value, and put txtname. the value of text is assigned to it, added to the "output stream", and response. redirect outputs to another webpage.

Then let's see how to retrieve the value of this cookie on the target page:

 

If (Request. Cookies [ " Name " ] ! =   Null )
Label3.text = Request. Cookies [ " Name " ]. Value;

 

Obviously, the steps are very similar to the previous method, except that request. Cookies are used to replace request. querystring.

Note: Some browsers do not support cookies.

 

3. Session Variables

 

Next, let's take a look at the session variables maintained on the server. The session is created when the user sends the first request to the server, and terminated when the user closes the browser or suffers an exception (in fact, there is still an expiration ). The following code is an example of using session to transmit values. We can see that the session creates the "name" key for the user and assigns the textbox value to it.

Code
// Session Creation
Session [ " Name " ] = Txtname. text;
Response. Redirect ( " Webform5.aspx " );

// the following code shows how to set a value from the session
/// place the code on other pages

If (session [ " name " ] ! = null )
label3.text = session [ " name " ]. tostring ();

 

4. Application variable

 

Sometimes, we need a value that can be accessed on all pages. In this case, we can use the application variable. As shown in the following code, once an application variable is created and assigned a value, it can be obtained on all pages of the website (project.

Code
// Assign values to Application variables

Application [ " Name " ] = Txtname. text;
Response. Redirect ( " Webform5.aspx " );

//Retrieve value from application variable

If(Application ["Name"]! = Null)
Label3.text=Application ["Name"]. Tostring ();

 

5. server. transfer (or httpcontext)

 

We can also use server. the transfer mode (or httpcontext mode) transmits variables between pages. In this case, the variables to be passed can be obtained through attributes or methods. It is easier to use attributes. Well, let's write an attribute on the first page to obtain the textbox value:

 

Code
Public StringGetname
{
Get{ReturnTxtname. Text ;}
}

 

We need to use server. Transfer to send this value to another page. Please note that server. transfer only sends the control to a new page and does not redirect the browser to another page. Therefore, we still see the original page URL in the address bar. The following code is used:

 

Server. Transfer ( " Webform5.aspx " );

 

Next, let's take a look at "webform5.aspx:

 

Code
// You can declare this globally or in any event you like

Webform4 W;

//Gets the page. context which is associated with this page

W=(Webform4) Context. Handler;
//Assign the label control with the property "getname" which returns string

Label3.text=W. getname;

 

Conclusion:

 

As we can see, each method has its own advantages and disadvantages. It is very important to select an appropriate method under different circumstances.

 

 

 

The first translation is not well written. Please forgive me a lot

 

 

Sincerely,
Lance Zhang
Microsoft online community support

Please remember to click "mark as answer" on the post that helps you. This can be beneficial to other community members reading the thread.

 

 

 

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.