简体   繁体   English

在.net中每个会话仅显示一次文本

[英]Display text only once per session in .net

I have a text to be displayed to user only once per session ie only the first time they try to open a page. 我有一个文本只能在每个会话中显示给用户一次,即仅在他们第一次尝试打开页面时。 If they try to open again they are not supposed to see that text. 如果他们试图再次打开他们不应该看到该文本。

If they close the browser and open it again they should be able to see the text again. 如果他们关闭浏览器并再次打开它们,他们应该能够再次看到该文本。 I wanted to use session, but I am not sure how to use it. 我想使用session,但我不确定如何使用它。

I tried to use cookie which worked, but what if cookie is disabled, I guess this wont work. 我尝试使用有效的cookie,但是如果禁用cookie会怎样,我想这不会起作用。 so I decided to go with Sessions. 所以我决定和Sessions一起去。

Define OnSessionStart Event In Global.asax ...like this... Global.asax定义OnSessionStart事件......像这样......

 void Session_OnStart(object sender, EventArgs e) 
    {
       Session["showmessage"]="Show";
        }

On pageLoad or Event where you want to check... Check Session...if you want to show your text in label do like this... pageLoad或您要检查的事件...检查会话...如果您想在标签中显示您的文字,请执行以下操作...

 protected void Page_Load(object sender, EventArgs e)
    {
if(Session["showmessage"].ToString()=="Show")
{
Label1.Text="Message";//i Supposed you wana Show Message in Label.You Can Write your Code to Show Message wherever you wnat show.
Session["showmessage"]=Not Show";//To Display Message Only One Time.
}

Note-: It will Display Message Every Time Your Session Expired. 注意:每次会话过期时都会显示消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM