asp.net cookie 读取不起作用(同一页面)

asp.net cookie reading does not functioning (same page)

我有一个带有按钮和文本框的页面。 我原以为应该在页面加载时读取以前的 cookie。 只是尝试 read/write 简单的例子。按钮应将文本框值保存到 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);
    }

您可能应该使用值 属性。试试这个:

    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);
}