Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
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 27-Jan-13 18:40pm
Comments
shabari7 at 27-Jan-13 23:46pm
   
response.redirect or server.transfer?
shabari7 at 27-Jan-13 23:49pm
   
I think u need to assign in page load event

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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.
  Permalink  
Comments
Marcus Kramer at 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.
kshitij_CodeMaster at 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)

  Print Answers RSS
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 Raul Iloc 170
4 Maciej Los 164
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100