Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As you can see, the web page gets a cookie named "asdf" and puts the string value in textbox1. I then parsed the string to take out the "asdf=" and place the parsed string in textbox2.

I want the web page user to be able to edit the string in textbox2, and have that saved as the cookie but this isn't working using the SetCookie procedure.

Thanks in advance.

Here is the c# source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace WebApplication23
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string cookieString = GetCookie("asdf");
            TextBox2.Text = cookieString;
            TextBox3.Text = "";
            parseData(cookieString, "asdf=", TextBox3);
        }
        //-------------------------------------------------------------
        private void parseData(string cookieString, string pattern, TextBox txt)
        {
            int b = 0;
            int c;
            string[] substrings = Regex.Split(cookieString, pattern);
            foreach (string match in substrings)
            {
                if (substrings[b] != "")
                {
                    substrings[b] += "";// +Environment.NewLine;
                    }
                b++;
            }
            for (c = 0; c < b; c++)
            {
                TextBox3.Text+=substrings[c];
            }
        }
        //-------------------------------------------------------------
        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox2.Text = "";
            TextBox2.Text=GetCookie("asdf");
        }
        //-------------------------------------------------------------
        public string GetCookie(string cookiename)
        {
            string cookyval = "";
            try
            {
                cookyval = Request.Cookies[cookiename].Value;
                //cookyval = Request.Cookies[cookiename].Name;
            }
            catch (Exception e)
            {
                cookyval = "";
            }
            return cookyval;
        }
        //-------------------------------------------------------------
        protected void Button1_Click(object sender, EventArgs e)
        {
            SetCookie("asdf", TextBox3.Text, 1);
        }
        //-------------------------------------------------------------      
        
        
        
        
        //-------------------------------------------------------------
        public bool SetCookie(string cookiename, string cookievalue, int iDaysToExpire)
        {
            try
            {
                HttpCookie objCookie = new HttpCookie(cookiename);
                Response.Cookies.Clear();
                Response.Cookies.Add(objCookie);
                objCookie.Values.Add(cookiename, cookievalue);
                DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
                Response.Cookies[cookiename].Expires = dtExpiry;
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {
            SetCookie("asdf", TextBox3.Text, 1);
        }
    }
}
Posted
Updated 10-Aug-10 2:37am
v2

1 solution

Try this:
C#
HttpCookie objCookie = new HttpCookie(cookiename);
objCookie.Values.Add(cookiename, cookievalue);
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
Response.Cookies.Clear();
Response.Cookies.Add(objCookie);
Response.Cookies[cookiename].Expires = dtExpiry;


I have just changed the order of attaching the cookie to the response. Once you have make the Cookie, add values to it then add it to the response.
 
Share this answer
 
Comments
bulbus aramus 10-Aug-10 10:45am    
didn't work, it's behaving the same way
Sandeep Mewara 10-Aug-10 11:28am    
Can you elaborate the behaviour? What is happening and how are you checking it? Further, try no-expire case for once.

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