[英]Show banner once per browser session using asp.net and cookies
我希望每個瀏覽器會話僅顯示一次橫幅。
<asp:Panel ID="Panel1" runat="server">
<img src="path"/>
</asp:Panel>
我想使用來自代碼隱藏文件的cookie實現此目的。
我的代碼在下面,但橫幅始終顯示,我如何以投注方式實現此目標
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Set Banner Cookie
HttpCookie BannerCookie = new HttpCookie("ShowBanner");
BannerCookie.Value = "YES";
Response.Cookies.Add(bannercookie)
Panel1.Visible = False;
//Do Somthing...
ShowPageDetails();
ShowBanner();
}
else
{
//Do Somthing
Panel1.Visible = False;
ShowPageDetails();
ShowBanner();
}
}
public void ShowBanner()
{
HttpCookie BannerCookie = Request.Cookies["ShowBanner"];
if (BannerCookie != null)
{
Panel1.Visible = True;
BannerCookie.Value = null;
Response.Cookies.Add(BannerCookie);
}
else
{
Panel1.Visible = false;
}
}
更新:我嘗試了下面由Ajay
提到的解決方案,但是該解決方案無法正常工作,要么在cookie為null時生成錯誤,要么總是顯示橫幅。
我不確定如何更改邏輯,以便橫幅廣告在每個瀏覽器會話中僅顯示一個。 我嘗試了幾種方法,但沒有成功。 任何其他可能從背后的代碼解決方案。
您需要糾正ShowBanner()邏輯,當不存在cookie時顯示您不做反向操作,也在頁面加載時不要每次都添加cookie
我希望每個瀏覽器會話僅顯示一次橫幅。
我想使用來自代碼隱藏文件的cookie實現此目的。
我的代碼在下面,但橫幅始終顯示,我如何以投注方式實現此目標
public void ShowBanner()
{
HttpCookie BannerCookie = Request.Cookies["ShowBanner"];
if (BannerCookie != null)
{
Panel1.Visible = false;
}
else{
Panel1.Visible = true;
}
}
在頁面加載時,僅在不存在時添加cookie
HttpCookie BannerCookie = Request.Cookies["ShowBanner"];
if (BannerCookie == null)
{
BannerCookie = new HttpCookie("ShowBanner");
BannerCookie.Value = "YES";
Response.Cookies.Add(bannercookie)
}
創建一個會話變量:
if(Session["whatever"] == null)
{
// show the post
}
else
{
Session["whatever"] = 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.