1

I am using cookies to know whether a page was loaded before or not. So in page load of a asp.net c# page I am using this

if (Request.Cookies["PageLoaded"] == null)
{
   //Initialize things if page loading for first time.
}

and inside the if as last parameter I am setting the cookies value like given below

if (Request.Cookies["PageLoaded"] == null)
{
   //Initialize things if page loading for first time.

   //Set cookies value to indicate page has loaded before
   Response.Cookies["PageLoaded"].Value = "True";
}

When I run in local host its working fine. But when I host it to server for each page load(Postback events) the initial if statement is true(ie cookie is always null) and going inside the loop.

Am I doing something wrong? How can I do this in c#? Thanks

Zach
  • 9,989
  • 19
  • 70
  • 107

1 Answers1

3

Try setting an expiry date for your cookie, by default if you do not set an expiry date for the cookie it will be non-persistant and only stored as part of the Session information so when you close the browser the Cookie will be discarded e.g.

Response.Cookies["PageLoaded"].Value = "True";
Response.Cookies["PageLoaded"].Expires = DateTime.Now.AddDays(1);
James
  • 80,725
  • 18
  • 167
  • 237
  • Thanks for the reply. But not a success. – Zach Sep 13 '11 at 09:58
  • @Leppie - perhaps he just never closed the browser! – James Sep 13 '11 at 10:08
  • @Leppie - yeah but the fact that he says it is working on localhost indicates the Session State is enabled (at least on the dev server). Looking at his comment to your question it does appear it is enabled. Another suggestion might be to make sure you have cookies enabled in the browser. – James Sep 13 '11 at 10:53