Question

I am setting cookie value in a usercontrol[A] and reading the value in the another usercontrol [B].

But the value is only available on page refresh in the server side. I can see the updated value in firebug cookie tab.

If i refersh the page the correct value is diplaying inthe page.

How to fix this issue?Below is the code i am using to read the cookie in usercontrol[B]. Its always the old value not the new value that i set in the usercontrol[A]

 HttpCookie cookieTool = Request.Cookies["previousTool"];

    string strSessionReturnToolTitle = "";
    string strSessionReturnToolURL = "";

    if (cookieTool != null)
    {
     //   Response.Write("<BR>Cookie value  " + cookieTool["returnToolurl"].ToString());
        if (Request.UrlReferrer == null)
        {
            cookieTool.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(cookieTool);
        }
        else
        {

            strSessionReturnToolTitle = cookieTool["returnTooltitle"];
            strSessionReturnToolURL = Server.UrlDecode(cookieTool["returnToolurl"]);
        }
    }
Was it helpful?

Solution

Request.Cookies is incoming. Response.Cookies is outgoing.

Request.Cookies only knows about the current request. I don't think it updates until the following request when you add via Response.Cookies.

You could try getting the cookie via Response.Cookies["previousTool"] in case Request.Cookies["previousTool"] is null.

If that doesn't work you'll need another way, such as storing the value in the Session or HttpContext.Current.Items.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top