The "(String)", "" of the Session in C #. ToString () and "convert.tostring" usage notes
In practice, we often encounter the value of the Session to a String to determine whether it is empty or to determine whether there is permission to access a page, where the conversion process if the improper use will throw an exception, to the visitor a bad user experience. Here I write it as a note for reference.
First, when session["a"] = = NULL,
Session["a"]. ToString () throws an exception;
(string) Session["A"] is null;
Convert.ToString (session["a"]) is "".
Second, when session["a"] = = "",
Their values are "".
Therefore, when judging whether session["a"] has a value, use ". ToString () ", then it must be written in the following format and order:
if (session["a"]! = null && session["a"]. ToString ()! = "")
Here, you should pay attention to the order of Judgment: first determine whether it is null, and then determine whether it is empty. If session["a"] is null, then session["a"]! = NULL to FALSE will not be executed naturally. ToString () does not give an error, and if session["a"] is not NULL, it is executed. ToString () also does not error.
Similarly if (session["a"] = = NULL | | Session["a"]. ToString () = = "") This sentence is also legally available.
Use. The method of ToString () is relatively fixed, and if it is written in (string), it is more free:
if (string) session["a"]! = null && (String) session["a"]! = "")
if (session["a"]! = null && (String) session["a"]! = "")
Both of these are feasible and have no relation to the order in which null and empty are judged.
The simplest way is to use convert.tostring
if (convert.tostring (session["AAA"]) = = "")
Whether session["a"] is null or empty,convert.tostring (session["AAA") is empty.
Session validity time Default is 20 minutes, the content from the Chinese Webmaster Information Network (www.chinahtml.com) This time refers to when the user logged in, if there is no action, that is, no request on the web. The time to maintain the identity of this user is 20 minutes, If the user has been requested to work with the web, then this time has not been counted within 20 minutes, that is, if the user logged in, even if the operation one hours, then there will be no time-out, unless the user and the server dropped the line
Session configuration information in the Web. config file
Mode= "InProc"
Stateconnectionstring= "tcpip=127.0.0.1:42424"
sqlconnectionstring= "Data source=127.0.0.1; Trusted_connection=yes "
Cookieless= "false"
Timeout= "20"
/>
Using session objects to pass values between pages
The session object can be used to pass values between pages, but it is important to note that too much data cannot be stored in the session object, otherwise the server will be overwhelmed and the object should be released in a timely manner when the Seesion object is no longer needed.
For example, use the Session object to transfer the user's login name, and on another page, display the user's login name.
The code to save the user's login name using the Session object is as follows:
Session.remove ("UserName"); session["UserName"] = txtName.Text; Response.Redirect ("navigatepage.aspx"); |
In the Navigatepage.aspx page, the value of the Session object is displayed on the interface with the following code:
if (session["UserName"] = = null) { Response.Redirect ("default.aspx"); } Else { Label1.Text =session["UserName"]. ToString (); } |
Use session to authenticate user login
In ASP. NET, it is very simple to use the Session object to verify that the user is logged in, for example, in this instance, when the user is logged in safely, the user's login name can be saved with the following code and jump to navigatepage.aspx.
if (txtName.Text = = "Mr" && txtpassword.text = = "Mrsoft") { session["UserName"] = TxtName.Text.Trim (); Response.Redirect ("navigatepage.aspx"); } |
In the Navigatepage.aspx page, when the page loads, first determine if there is a value in the Session object, if there is a value, then do something else; otherwise, return to the login page. The main code is as follows:
if (session["UserName"] = = null) Response.Redirect ("default.aspx"); Else { Other operations } |
Note: The default expiration period for the session object is 20 minutes, and the user can also set it in Web. config with the following code:
"Reprint" The use of the session in C #