这是问题,首先我将复制代码,然后我将解释问题是什么。
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;
}
}
所以在页面加载时有面板并询问客户端是否接受cookie如果单击是转到OKButton,并且在cookie存储在浏览器中后(我可以在资源中看到它们)以及当我点击其他页面时网站,然后面板出现againg并问我饼干。这是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 :(得分:0)
尝试在您的Page_Load代码周围添加!IsPostBack()
,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack())
CheckIfCookieExists();
}
我认为如果你在非回发上设置/检查cookie,那么cookie应该保留。