Several methods for transferring values on the Asp.net page

Source: Internet
Author: User

In this article Article Azamsharp will introduce some methods for passing 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) CopyCode The Code is as follows: Response. Redirect ("webform5.aspx ");

First, let's take a look at the response. Redirect method, which should be the simplest. When we click the response. Redirect button, we can trigger the above line of code. 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:Copy codeThe Code is 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.Copy codeThe Code is as follows: 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.Copy codeThe Code is as follows: 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:Copy codeThe Code is as follows: 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:Copy codeThe Code is as follows: 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.Copy codeThe Code is as follows: // session Creation
Session ["name"] = txtname. text;
Response. Redirect ("webform5.aspx ");
// The following code shows how to set the value from the session
// Put 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.Copy codeThe Code is as follows: // assign a value to the application variable
Application ["name"] = txtname. text;
Response. Redirect ("webform5.aspx ");
// Retrieve the value from the 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:Copy codeThe Code is as follows: Public String getname
{
Get {return txtname. 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:Copy codeThe Code is as follows: // 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.

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.