2013-09-30 36 views
0

這裏是問題,首先我將複製代碼,然後我會解釋什麼問題。創建定製餅乾

protected void Page_Load(object sender, EventArgs e) 
{ 

    CheckIfCookieExists(); 

} 

bool CheckIfCookieExists() 
{ 

    HttpCookie cookie = new HttpCookie("accpetedCookie"); 
    cookie.Values.Add("username", "user"); 
    cookie.Expires = DateTime.Now.AddYears(1); 
    Response.Cookies.Add(cookie); 

    HttpCookie myCookie = Request.Cookies["accpetedCookie"]; 
    if (myCookie == null) 
    { 
     return false; 

    } 
    if (!string.IsNullOrEmpty(myCookie.Values["username"])) 
    { 
     return true; 

    } 
    else 
    { 
     return false; 
    } 

} 
protected void OKButton_Click(object sender, EventArgs e) 
{ 
    if (!CheckIfCookieExists()) 
    { 
     pnlDialog.Visible = true; 
    } 
    else 
    { 
     pnlDialog.Visible = false; 
    } 
} 

等頁面加載有面板,並要求客戶你接受cookies,如果點擊是去OKButton和餅乾都存儲在瀏覽器之後(我可以看到他們的資源),當我點擊其他頁面在同一個網站上,然後面板出現againg並詢問我的cookie。這是HTML方面。

<div id="dialog-content"> 
    <asp:Panel ID="pnlDialog" title="Cookies policy" runat="server"> 
     <div id="dialog"> 
     <p> 
      If you agree to accept cookies click Yes, otherwise click No!</p> 
     <div class="popup-btn-content"> 
      <div class="popup-ok-btn"> 
       <asp:Button runat="server" Text="Yes" ID="OKButton" onclick="OKButton_Click" 
        style="height: 26px" /></div> 
      <div class="popup-exit-btn"> 
       <asp:Button runat="server" Text="No" ID="ExitButton" 
        onclick="ExitButton_Click" OnClientClick="return hidePanel()" /></div> 
     </div> </div> 
    </asp:Panel> 
    </div> 
+0

你確定你已經添加了關於「我會解釋什麼問題是」的部分嗎?我的帖子中沒有看到任何問題...(請注意,您的代碼是向後的,因爲它首先將Cookie添加到Response,它會自動複製到請求,並且比您檢查始終存在的Cookie存在)。 –

回答

0

嘗試把一個!IsPostBack()在你的Page_Load代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack()) 
     CheckIfCookieExists(); 
} 

我認爲,如果你設置/檢查不回發的餅乾,餅乾應該保持。