I really appreciate you staying with this.
Could you please address tyhe following?
Run three statements one afrter the other:
Response.cookie.add("Something", "one")
Response.cookie.add("Something", "two")
x=Response.cookie("Something")
I used Request last time - I meant Response.
I believe the above returns "one" - not sure.
So a remove before the second add might make sense.
No?
First, you can't do it like you are stating here, at least not in the newer
versions of ASP.NET.
Second, you are not rewriting the cookie if you use Cookie.Add() as you are
creating two cookies named Something.
Here is some sample code:
PAGE
---------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function confirmButton() {
return confirm('Are you sure everything is correct?');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="CookieLabel" runat="server" Text="Label"></asp:Label><br
/>
<asp:Button ID="submitButton" runat="server" Text="Click Me!"
onclick="submitButton_Click" OnClientClick="confirmButton" />
</form>
</body>
</html>
---------------------------------------------------
Code
---------------------------------------------------
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Cookies["Something"].Value = "one";
Response.Cookies["Something"].Value = "two";
HttpCookie cookie = Response.Cookies["Something"];
CookieLabel.Text = cookie.Value;
}
}
protected void submitButton_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["Something"];
CookieLabel.Text = cookie.Value;
}
}
---------------------------------------------------
In each case, the cookie label reads "two", as the cookie is reset. If you
want cookie add, you have this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Cookies.Add(new HttpCookie("Something", "one"));
Response.Cookies.Add(new HttpCookie("Something", "two"));
HttpCookie cookie = Response.Cookies["Something"];
CookieLabel.Text = cookie.Value;
}
}
In this case, the answer is one, as you have two cookies named Something.
Want proof?
Change the form to this
<form id="form1" runat="server">
<asp:Label ID="CookieLabel" runat="server" Text="Label"></asp:Label><br />
<asp:Button ID="submitButton" runat="server" Text="Click Me!"
onclick="submitButton_Click" OnClientClick="confirmButton" /><br />
<asp:Label ID="CountLabel" runat="server" Text="Label"></asp:Label>
</form>
And the Page_Load() to this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Cookies.Add(new HttpCookie("Something", "one"));
Response.Cookies.Add(new HttpCookie("Something", "two"));
HttpCookie cookie = Response.Cookies["Something"];
CookieLabel.Text = cookie.Value;
CountLabel.Text = Response.Cookies.Count.ToString();
}
}
Answer == 2.
Now change submit button click to
protected void submitButton_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["Something"];
CookieLabel.Text = cookie.Value;
CountLabel.Text = Request.Cookies.Count.ToString();
}
And one of the cookies disappears. Why? Because the browser reads that you
have two cookies named Something. It stores cookie 1 ("one") and then
stores cookie 2 over cookie 1. The page now reads:
"two"
1
Copy the code here and play with it. Cookies are not really that hard once
you get the idea that the cookie is stored on the client during a RESPONSE
and sent to the server on a REQUEST.
This is why my Page_Load() initially sets the cookie and the button click
retrieves. I MUST complete one trip completely before the cookie is stored
on the client as I have to send something to the client.
Let's change this a bit and see what happens. Make Page_Load read this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HttpCookie cookie1 = Request.Cookies["MyCookie"];
if(cookie1 != null)
CookieLabel.Text = cookie1.Value;
CountLabel.Text = Request.Cookies.Count.ToString();
Response.Cookies.Add(new HttpCookie("MyCookie", "one"));
Response.Cookies.Add(new HttpCookie("MyCookie", "two"));
HttpCookie cookie2 = Request.Cookies["MyCookie"];
if(cookie2 != null)
CookieLabel2.Text = cookie2.Value;
CountLabel2.Text = Request.Cookies.Count.ToString();
}
}
What is expected here?
Cookie1 is null
CookieLabel = ""
CountLabel = 0
Cookie2 = "one"
CookieLabel = "one"
CountLabel = 2
Why? There is some magic in that old top hat they found.
1. Microsoft adds the Response cookies to the Request collection so you can
retrieve. This is actually not a good thing, but it protected newbs from
themselves. In reality, the cookie has not been set on the client, so this
is cheating.
Now, let's call the page again. What can we expect?
Cookie1 is null
CookieLabel = "two"
CountLabel = 1
Cookie2 = "two"
CookieLabel = "one"
CountLabel = 3
What just happened here?
1. The first cookie in the Request collection came from the client and has
"two" in it.
2. The second cookie in the Request collection was the Cookie added with
this line:
Response.Cookies.Add(new HttpCookie("MyCookie", "one"));
3. The third cookie in the Request collection was the Cookie added with
this line:
Response.Cookies.Add(new HttpCookie("MyCookie", "two"));
Examine:
Response collection = 2 cookies
Request collection = 3 cookies
Does this make sense now. Let me restate your question.
Why, if I was overwriting the cookie like this:
Response.cookie.add("Something", "one")
Response.cookie.add("Something", "two")
x=Response.cookie("Something")
am I getting "one" instead of "two". Do you see the reason why now?
Peace and Grace,
--
Gregory A. Beamer (MVP)
Twitter: @gbworld
Blog:
http://gregorybeamer.spaces.live.com
*******************************************
| Think outside the box! |
*******************************************