Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems updating cookies 1

Status
Not open for further replies.

jgd1234567

Programmer
May 2, 2007
68
GB
Hi, i'm having some problems updating cookies. I have the following code when a user submits a comment:

Response.Cookies["Comment"]["FullName"] = txtFullName.Text;
Response.Cookies["Comment"]["Email"] = txtEmail.Text;
Response.Cookies["Comment"]["Url"] = txtUrl.Text;
Response.Cookies["Comment"].Expires = DateTime.Now.AddYears(1);

And then in my page load i have:

if (Request.Cookies["Comment"] != null)
{
txtFullName.Text = Request.Cookies["Comment"]["FullName"];
txtEmail.Text = Request.Cookies["Comment"]["Email"];
txtUrl.Text = Request.Cookies["Comment"]["Url"];
}

The first time i post a comment the cookie is successfully set but if i post a new comment and change one of values the cookie doesn't get updated.

I have just read the msdn documentation and it suggests that my could should work for both creating and updating the cookie but it doesn't seem to update.

Appreciate your help. Thanks
 
have you tried removing the cookie and adding a new one.
Code:
protected void ButtonClick(sender object, eventargs e)
{
   if(cookies["comment"] != null)
   {
      cookies.remove("comment");
   }

   object[] data = new object[] { 
      txtfullname.text,
      txtemail.text,
      txturl.text 
   };
   cookies.Add("comment", data);
}

protected void PageLoad(sender object, eventargs e)
{
   if (!ispostback)
   {
      object[] data = (object[])cookies["comment"];
      if(data != null && data.length == 3)
      {
         txtfullname.text = data[0].tostring();
         txtemail.text = data[1].tostring();
         txturl.text = data[2].tostring();
      }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi, i'm a plonka. I forgot to wrap if (!this.IsPostBack) around the first part in the page load so the contents were always getting overwritten.
 
Actually, jmeckley emphasized an important point. Your cookies won't update unless you re-add them to the Response.Cookies collection.

It sounds like you must be doing that, but it's nice to note.

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top