In this post i want to show how to create a cookie in asp.net page which contains a counter ,This counter will be increased every time you visit the page.
The tip which i want to highlight here-(I got this question from one of my firends),That when you try to update the cookie value in asp.net you have to update the expiry date of the httpcookie object,otherwise the asp.net will store the default value which is : 1/1/0001,which let your code to create a cookie every time instead of update the counter value in the cookie file.
The code to do this on page_load event in C# :
if (Request.Cookies["counter"] != null)
int x = int.Parse(coo.Value);
x += 1;
coo.Value = x.ToString();
coo.Expires = DateTime.Now.AddDays(7);// if you omit this line,the cookie will be created each time.
HttpCookie coo = new HttpCookie("counter", "0");
coo.Expires = DateTime.Now.AddDays(7);
The code for this post is uploaded here:
Hope this helps.