开发者

Reading Cookies in C# and ASP.net; the cookie values lags behind

I'm having difficulty getting the updated value of the getCookie the second, third, fourth etc. time I change the value of either my txtPrice or ddTaxRate. It works fine on the first time I hit the calculation开发者_如何学C but if I change the value of either the txtPrice or the ddTaxRate then I need to hit the calculation button twice to get an updated value for the getCookie.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //First time the page loads
        }
        else
        {
            HttpCookie myCookie = new HttpCookie("data");
            myCookie.Expires = DateTime.Now.AddHours(12);

            //Grab data
            myCookie.Values.Add("price", txtPrice.Text);
            myCookie.Values.Add("tax", ddTaxRate.SelectedItem.Value.ToString());
            Response.Cookies.Add(myCookie);

            calculate();
        }
    }

    protected void cmdCalculate_Click(object sender, EventArgs e)
    {
    }

    protected void calculate()
    {
        if (Request.Cookies["data"] != null)
        {
            HttpCookie getCookie = Request.Cookies["data"];

            double price = Convert.ToDouble(getCookie.Values["price"]);
            double taxRate = (Convert.ToDouble(getCookie.Values["tax"]));
            double rate = taxRate / 100;
            double total = (price + (price * rate));

            txtNetPrice.Text = Convert.ToString(total);
        }
    }
}

I've seen the getCookie lag behind in value through debugging. Was wondering if anyone knows how to get an up-to-date value of the getCookie?


Request.Cookies contains the cookies that were sent in the request.

When you add a cookie to the response, it doesn't show up in Request.Cookies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜