| Address: http://www.codeproject.com/KB/aspnet/TransferingValues.aspx
Introduction:
In the development of Web applications, passing values between pages should be the most common problem. In this 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)
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:
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 temporary response variable Flag It carries the txtname value.
privatevoidButton1_Click(objectsender,System.EventArgse)
{
//ValuesentusingHttpResponse
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:
HttpCookiecName=newHttpCookie("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
// Create a session
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.
Code
// Assign values 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:
Code
publicstringGetName
{
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
//YoucandeclarethisGloballyorinanyeventyoulike
WebForm4w;
//GetsthePage.ContextwhichisAssociatedwiththispage
w=(WebForm4)Context.Handler;
//AssigntheLabelcontrolwiththeproperty"GetName"whichreturnsstring
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. |