我在第二次、第三次、第四次等更改 txtPrice 或 ddTaxRate 的值时,很难获得 getCookie 的更新值。它在我第一次点击计算时工作正常,但如果我更改 txtPrice 或 ddTaxRate 的值,那么我需要点击计算按钮两次以获得 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);
}
}
}
我通过调试发现 getCookie 的值落后。想知道是否有人知道如何获取 getCookie 的最新值?
请您参考如下方法:
Request.Cookies 包含在请求中发送的 cookie。
当您向响应添加 cookie 时,它不会显示在 Request.Cookies 中。
