这是问题所在,首先我将复制代码然后我将解释问题所在。
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;
}
}
所以在页面加载时有一个面板并询问客户端如果单击是转到 OKButton 是否接受 cookie,并且在 cookie 存储在浏览器中(我可以在资源中看到它们)并且当我单击同一站点上的其他页面时,然后面板再次出现并要求我提供 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>