值未存储在Cookie中

时间:2017-06-15 06:15:10

标签: c# cookies

我在将值存储在Cookie中时遇到问题。由于某种原因,它正在工作然后停止了。它给了我一个空例外。即使我在文本框中放了一些东西,我仍然会得到例外。

按下按钮的时间以及如何设置cookie的代码:

protected void submitBtn_Click(object sender, EventArgs e)
    {

        HttpCookie cookie = new HttpCookie("Information");


        cookie["First Name"] = firstTxt.Text;
        cookie.Expires = DateTime.Now.AddYears(1);
        Response.Cookies.Add(cookie);

        cookie["Last Name"] = lastTxt.Text;
        cookie.Expires = DateTime.Now.AddYears(1);
        Response.Cookies.Add(cookie);



    }

如何在第二页调用cookie

HttpCookie aCookie = Request.Cookies["Information"];


        infoLbl.Text = "The page you came from is called: " + PreviousPage.Title + "<br>";
        infoLbl.Text += "Your first name is: " + aCookie["First Name"] + "<br>";
        infoLbl.Text += "Your last name is: " + aCookie["Last Name"] + "<br>";
        infoLbl.Text += "The page has been loaded: " + count.ToString() + " times";

在第二页上,我在aCookie上收到一个空例外[&#34;名字&#34;]

0 个答案:

没有答案