Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi..
I have a dropdownlist in page1.I select the item that I want and click on go..It takes me to the next page(page2) and gets me the result..Again when I come back to page 1 the dropdownlist shows the default value and not the one which I selected..The same thing happens when I log out of my application and logs in..all that I wanted is dropdownlist should show the same value which I selected until and unless I close the browser.i have stored the dropdownlist in cookie..But I'm bit worried how to populate it when I log out nd login to my application or when i go some other page and come back to the page where I have given the condition..this is what I have done


HttpCookie myCookie = new HttpCookie("MyTestCookie");
DateTime now = DateTime.Now;

// Set the cookie value.
myCookie.Value = now.ToString();
// Set the cookie expiration date.
myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire

// Add the cookie.
Response.Cookies.Add(myCookie);

Response.Write("<p> The cookie has been written.");


//Read cookie

HttpCookie myCookie = new HttpCookie("MyTestCookie");
myCookie = Request.Cookies["MyTestCookie"];

// Read the cookie information and display it.
if (myCookie != null)
Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);
else
Response.Write("not found");
Posted
Comments
[no name] 27-Jan-13 23:46pm    
response.redirect or server.transfer?
[no name] 27-Jan-13 23:49pm    
I think u need to assign in page load event

1 solution

Before creating cookie you need to check whether that cookie with tat name already exits or not.Right now you are creating cookie without checking whether it is already there in client side. thats mean ur cookie value get override each time.
Put ur code like this:

XML
HttpCookie myCookie = new HttpCookie("MyTestCookie");
 myCookie = Request.Cookies["MyTestCookie"];

 // Check for cookie already present
 if (myCookie != null)
 Response.Write("Cookie is already there.");
 else
{
//create cookie

HttpCookie myCookie = new HttpCookie("MyTestCookie");
 DateTime now = DateTime.Now;
  
 // Set the cookie value.
 myCookie.Value = now.ToString();
 // Set the cookie expiration date.
 myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire
  
 // Add the cookie.
 Response.Cookies.Add(myCookie);
  
 Response.Write("The cookie has been written.");
}



Ur reading cookie code remains same as u hav written already
hope this helps u out.
 
Share this answer
 
Comments
fjdiewornncalwe 30-Jan-13 9:36am    
A couple of notes for you. Please don't use textspeak in your answers. We like to consider ourselves adults here at CP and textspeak just plain out drives us nuts.
[no name] 30-Jan-13 11:16am    
i will surely note down and follow the things that noticed and will try to improve them. thanx for helping me and improving me
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900