Cookie总是在asp.net中返回null

时间:2017-01-09 11:58:30

标签: asp.net cookies

净网页,在第一篇,我写的如下:

protected void lbXML_Click(object sender, EventArgs e)
{
    HttpCookie cookie = new HttpCookie("RequestedXML");
    cookie["xmlContent"] = hide_XML.Value;
    Response.Cookies.Add(cookie);
    cookie.Expires = DateTime.Now.AddMinutes(20);
    Response.Write("<script>window.open('XML_Editor.aspx', '_blank', 'toolbar=no, location=no, resizable=yes, width=800px, height=500px', true);</script>");
}

在XML_Editor.aspx中,我写了这样的代码:

protected void Page_Load(object sender, EventArgs e)
{
    Page lastPage = (Page)Context.Handler;
    if (!IsPostBack)
    {

        HttpCookie cookie = Request.Cookies["RequestedXML"];
        if (cookie != null)
        {
            TextBox1.Text = cookie["xmlContent"];
        }

    }
}

问题是cookie总是在XML_Editor.aspx的Page_Load中向我返回null,为什么会这样?

1 个答案:

答案 0 :(得分:0)

你的问题可能是你的代码,试试看:

 HttpCookie cookie = new HttpCookie("RequestedXML");
            cookie.Value =hide_XML.Value;

            cookie.Expires = DateTime.Now.AddMinutes(20);
            Response.Cookies.Add(cookie);
            Response.Write("<script>window.open('XML_Editor.aspx', '_blank', 'toolbar=no, location=no, resizable=yes, width=800px, height=500px', true);</script>");

XML_Editor.aspx

Page lastPage = (Page)Context.Handler;
            if (!IsPostBack)
            {

                var cookie = Request.Cookies["RequestedXML"];
                if (cookie != null)
                {
                    Response.Write(cookie.Value);
                }

            }