In this article, Azamsharp will introduce some ways to ASP.net page values. The example presented here is very simple, containing only a text box and several buttons, and the string in the text box will be passed to another page in a different way when the button is clicked.
1. Response.Redirect (or Query String method, url method)
Copy Code code as follows:
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:
Copy Code code as follows:
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.
Copy Code code as follows:
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.
Copy Code code as follows:
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:
Copy Code code 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 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:
Copy Code code as follows:
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.
Copy Code code as follows:
Session creation
session["Name"] = txtName.Text;
Response.Redirect ("webform5.aspx");
The following code shows how to take a value from the session
The code is placed in another page
if (session["Name"]!= null)
Label3.text = session["Name". ToString ();
4. Application variable
Sometimes we need a value that can be accessed in all pages, when we can use the application variable. As shown in the following code, once we create the application variable and assign it, we can get it on all pages of the Web site (project).
Copy Code code as follows:
Assigning values to Application variables
application["Name"] = txtName.Text;
Response.Redirect ("webform5.aspx");
To remove a value from a application variable
if (application["Name"]!= null)
Label3.text = application["Name". ToString ();
5. Server.Transfer mode (or HttpContext way)
We can also use Server.Transfer (or HttpContext) to pass variables between pages, at which point the variables to be passed can be obtained by attributes or methods, and the properties will be easier to use. OK, let's write a property to get the TextBox value in the first page:
Copy Code code 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 simply sends the control to a new page without redirecting the browser to another page. So, what we still see in the Address bar is the URL of the original page. As shown in the following code:
Server.Transfer ("webform5.aspx");
Next, we go to "webform5.aspx" to see:
Copy Code code as follows:
Can declare this globally, or in any event, 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, it's important to choose the right way in different situations, because all the ways of passing values are good and bad.