asp.net cookie读取功能不正常(同页)

时间:2016-04-18 16:37:06

标签: c# asp.net cookies httpcookie

我有一个带有按钮和文本框的页面。 我期待之前的cookie应该在页面加载时读取。 只是尝试读/写简单的例子。按钮应该保存到cookie文本框值。在下一次开放时应该阅读它。但mycookie总是空的。这些代码有什么问题? 任何线索? (感谢)

    protected void Page_Load(object sender, EventArgs e)
    {


            HttpCookie mycookie = Request.Cookies["info"];

        if(mycookie!=null)
        TextBox1.Text=mycookie["mytext"];

    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        HttpCookie mycookie= new HttpCookie("info");
        mycookie.Expires = DateTime.Now.AddDays(3);

        mycookie["mytext"]=TextBox1.Text;


        Response.Cookies.Add(mycookie);
    }

1 个答案:

答案 0 :(得分:2)

您应该使用Value属性。试试这个:

    protected void Page_Load(object sender, EventArgs e)
{


        HttpCookie mycookie = Request.Cookies["info"];

    if(mycookie!=null)
    TextBox1.Text=mycookie.Value;

}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    HttpCookie mycookie= new HttpCookie("info");
    mycookie.Expires = DateTime.Now.AddDays(3);

    mycookie.Value=TextBox1.Text;


    Response.Cookies.Add(mycookie);
}