2014-03-31 59 views
0

我必須創建一個cookie,然後檢索存儲的cookie。創建和調用cookie

我該如何去做這件事?正在存儲的Cookie必須具有在「摘要」頁面上顯示的「我的愛好」頁面上鍵入的信息。

我必須保存cookie,然後重定向到Summary2並調用當我按下按鈕時存儲的cookie。下面是代碼我到目前爲止,我相信什麼,我問

protected void btnDisplay_Click(object sender, EventArgs e) 
    { 
     string vacations = Session["Vacations"] as string; 
     string hobbies = Session["Hobbies"] as string; 
     string classes = Session["Classes"] as string; 

     lblDisplay.Text = "Your favorite vacations spots are: " + vacations + "<br />" + 
     "Your hobbies are: " + hobbies + "<br />" + 
     "Your IT Classes are: " + classes; 

    } 
    protected void btnRedirect_Click(object sender, EventArgs e) 
    { 
     Response.Cookies["Hobbies"].Value = hobbies; 
     Response.Cookies["userName"].Expires = DateTime.Now.AddDays(30); 

     HttpCookie aCookie = new HttpCookie("lastVisit"); 
     aCookie.Value = DateTime.Now.ToString(); 
     aCookie.Expires = DateTime.Now.AddDays(1); 
     Response.Cookies.Add(aCookie); 

     Response.Redirect("Summary2.aspx", true); 
    } 

這裏相關的是什麼,我有程序來檢索我的Summary2網頁上的Cookie ...

protected void btnDisplay_Click(object sender, EventArgs e) 
    { 
     if (Request.Cookies["userName"] != null) 
      lblDisplay.Text = Server.HtmlEncode(Request.Cookies["userName"].Value); 

     if (Request.Cookies["userName"] != null) 
     { 
      HttpCookie aCookie = Request.Cookies["userName"]; 
      lblDisplay.Text = Server.HtmlEncode(aCookie.Value); 
     } 

    } 

我主要是不知道如何存儲在我的愛好頁面中輸入的信息。任何幫助,將不勝感激。謝謝!

回答

0

將「Cookie」放入您的Site.Master中,您可以隨時隨地訪問它。

0

我想出了我做錯了什麼。我在錯誤的按鈕命令中使用了Cookie方法,在Summary2中,我調用了錯誤的cookie名稱。