ASP .Net - How to restore fields

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

I need to restore first and lastname from cookie, please help. I am able to retreive the firstname, but can't get it to work when it comes to retrieving the second field (lastname). Here is what I have working for the first field: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) if (!(Request.Cookies["FirstName"] == null)) txtFirstName.Text = Request.Cookies["FirstName"].Value; Thanks

user01 · Nov 10, 2016 9:34 AM · 13,809 views

14 Replies

add your second field to cookie and retrieve it using same way you doin for first field.

nepalicr7 · Nov 10, 2016 10:03 AM

How do I go about including the second field? Do I need to create another Load?

newbie69 · Nov 10, 2016 10:20 AM

How do I go about including the second field? Do I need to create another Load? Need format plz

user01 · Nov 10, 2016 10:28 AM

bump

user01 · Nov 10, 2016 12:25 PM

तल हेर्नुस ! Last edited: 10-Nov-16 12:49 PM

Nas · Nov 10, 2016 12:32 PM

//Best way is to Create a cookie with multiple values assigned HttpCookie StudentCookies = new HttpCookie("StudentCookies"); Response.Cookies["StudentCookies"]["FirstName"] = "Nas"; Response.Cookies["StudentCookies"]["LastName"] = "Daju"; Response.Cookies.Add(StudentCookies); // protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack){ if (!(Request.Cookies["StudentCookies"]["FirstName"] == null)  && !(Request.Cookies["StudentCookies"]["LastName"] == null)) txtFirstName.Text = Request.Cookies["StudentCookies"]["FirstName"].Value; txtLastName.Text =Request.Cookies["StudentCookies"]["LastName"].Value; } } कुकिज बाहेक सेसन, भिउस्टेट, हिडन फिल्ड, क्य्वेरी स्त्रिंग पनि प्रयोग गर्न सक्न हुन्छ ! Last edited: 10-Nov-16 12:52 PM

Nas · Nov 10, 2016 12:46 PM

user01 · Nov 10, 2016 1:03 PM

Getting an error on line 34 now. I need the check out page to restore fname and lname. It worked using the first code for first field, but doesn't seem to work for the second field.

user01 · Nov 10, 2016 1:09 PM

After selecting continue it will go to check out page with items in cart. Next page also has btn that allows me to go back which is supposed to restore cached fname and lname and display it on respective fields. The problem I seem to have is that it only seems to work for the fname field

user01 · Nov 10, 2016 1:14 PM

Check the property of the textboxes. Enableviewstate set to True. Lets see if that solves the state issue

guitarcenter · Nov 10, 2016 1:59 PM

add {} braces when checking for cookies is null or not. when you load you page at first the cookies are not set that's why you are gettin that exception. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) if (!(Request.Cookies["FirstName"] == null) && !(Request.Cookies["LastName"] == null)) { TextBox1.Text = Request.Cookies["FirstName"].Value; TextBox2.Text = Request.Cookies["LastName"].Value; } }

nepalicr7 · Nov 10, 2016 2:00 PM

Also, remember if you have more than one line of code in you if ALWAYS PUT CURLY BRACES around if.

nepalicr7 · Nov 10, 2016 2:01 PM

Many Many thanks to everyone. {} did the trick. Thanks again.

user01 · Nov 10, 2016 2:09 PM

Yes cr7. There is no curly braces so its just taking one line after the condition. Lol

guitarcenter · Nov 10, 2016 2:09 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all