Using browser cookies

Source: Internet
Author: User

1. Create a cookie

<script runat= "Server" >
protected void btnAdd_Click (object sender, EventArgs e)
{
response.cookies["message"]. Value = txtcookievalue.text;//Creates a cookie for the contents of the text box input
}

</script>

<form id= "Form1" runat= "Server" >
<div >
<asp:label id= "Lblcookievalue" text= "Cookie Value:" associatedcontrolid= "Txtcookievalue" runat= "Server"/>
<asp:textbox id= "Txtcookievalue" runat= "Server"/>
<asp:button id= "Btnadd" text= "Add Value" onclick= "btnAdd_Click" runat= "Server"/>

</div>
</form>

2. Create a persistent cookie

<script runat= "Server" >
void Page_Load ()
{
Get Current value of cookie
int counter = 0;
if (request.cookies["Counter"]! = NULL)
{
Counter = Int32.Parse (request.cookies["counter"]. Value);
Increment counter
counter++;
ADD Persistent Cookie to browser
response.cookies["Counter"]. Value = counter. ToString ();
response.cookies["Counter"]. Expires = DateTime.Now.AddYears (2);
Display Value of Counter cookie
Lblcounter.text = counter. ToString ();
}
}

</script>

Properties of 3.Cookie

The HttpCookie class represents a cookie. When you create or read a cookie, you can use the following properties

(1) domain-is used to specify the domain name associated with the cookie, the default value is the current domain name;

(2) expires-is used to create a persistent cookie by specifying an expiration time;

(3) haskeys-is used to determine whether the cookie is a multi-valued cookie;

(4) httponly-is used to avoid cookies being accessed by JavaScript;

(5) name-The name of the user-specified cookie;

(6) The path-is used to specify the path associated to the cookie. The default is/.

(7) secure-is used to specify that the cookie needs to be passed through an SSL connection;

(8) value-allows the value of the read/write cookie;

(9) values-when using a multi-valued cookie, read/write a specific value;

Using browser cookies

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.