ここに問題があります。最初にコードをコピーしてから、問題が何であるかを説明します。
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>