asp.net cookies are not unfamiliar to readers who have used ASP, but we are talking about cookies in asp.net. We know that cookies exist in the user's computer browser and we can use cookies to store some very simple data. But, I don't want to mention that I didn't warn you: Remember this is not a good idea, because the user can delete the cookie information at any time, or turn off the cookie function. All right, that's the opening line.
Using ASP.net, we can easily manipulate the cookie collection. Like cookies in the ASP, it is attached to the request and response objects. Listing1 and 2 Give a method of how to read and write cookies, respectively. Figures 1 and 2 show the corresponding display.
Listing 1 The function of this file is to write cookies
<%@ language= "C #"%>
<HTML>
<script language= "C #" runat= "Server" >
void writeclicked (Object Sender, EventArgs e)
{
Creates a new cookie with a cookie name from the Namefield.text
HttpCookie cookie = new HttpCookie (namefield.text);
Set the value of a cookie
Cookie. Value = Valuefield.text;
Set cookie Life to 1 Minute,timespan () is a specially set time interval class, we define the fact that the example Tsminute
DateTime Dtnow = DateTime.Now;
TimeSpan Tsminute = new TimeSpan (0, 0, 1, 0);
Cookie. Expires = Dtnow + Tsminute;
Add cookies
RESPONSE.COOKIES.ADD (cookie);
Response.Write ("Cookie written. <br>}
</script>
<body>
Use the button below to write cookie to your browser
The cookie would expire in one minute.
<form runat= "Server" id= "Form1" >
Cookie Name
<asp:textbox id= "NameField" runat= "Server"/>
<br>
Cookie Value
<asp:textbox id= "Valuefield" runat= "Server"/>
<br>
<asp:button text= "Writecookie" runat= "Server" id= "Button1"/>
<br>
</form>
<a href= "readcookies.aspx" >read the cookies</a>
</body>
</HTML>
Figure 1
Listing 2 This file is to read the cookie value just written
<%@ language= "C #"%>
<script runat= "Server" >
void readclicked (Object Sender, EventArgs e)
{
Get the cookie name you want
String strcookiename = Namefield.text;
Gets the object that corresponds to this cookie name, and note that the current cookie is an object
HttpCookie cookie = Request.cookies[strcookiename];
Verify that the cookie already exists
if (null = = cookie) {
Response.Write ("Cookie not found. <br>}
else {
Display the value of a cookie
String Strcookievalue = cookie. Value.tostring ();
Response.Write ("the" + Strcookiename + "cookie contains: <b>")
+ Strcookievalue + "</b><br>}
}
</script>
<body>
Use the button below to read a cookie
<br>
<form runat= "Server" id= "Form1" >
Cookie Name
<asp:textbox id= "NameField" runat= "Server"/>
<asp:button text= "Readcookie" runat= "Server" id= "Button1"/>
</form>
<a href= "writecookies.aspx" >write cookies</a>
</body>
Figure 2
To better understand the read and write of cookies, code analysis is as follows.
In Listing 1, in order to write a cookie to the user's browser, we called the HttpCookie object and created an instance cookie for the HttpCookie object, and when the cookie was written, We used the Response.Cookies object's method Add (), and finally we set the lifetime of this cookie to one minute.
In Listing 2, we tried to read the cookie method. The simple thing is to access the Request.Cookies collection.
Cookies can only hold data of type string, and if you want to store more complex data types, you must first convert it to a string type. Tip One way to write a complex data type to a cookie is to convert the complex data type you want to store into an XML string and write a cookie. In addition, we can store more than one value in a cookie, this is not much to say, left to interested readers to study it.
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.