简体   繁体   English

会话和Page.IsPostBack

[英]Session and Page.IsPostBack

I have a website in ASP.NET. 我在ASP.NET中有一个网站。 I declare a session in page load to store the user ID and which will be empty by default. 我在页面加载中声明一个会话来存储用户ID,默认情况下该会话为空。

When the user clicks login the login page appears and the user logs in and user ID is stored in the session. 当用户单击登录时,将显示登录页面,并且用户登录并且用户ID存储在会话中。

When I return to the index page it disappears. 当我返回索引页面时,它消失了。

Here is my code: 这是我的代码:

if (!Page.IsPostBack)
{
    Session["UserID"] = "";
}

if (Session["UserID"] == "")
{
    HP_User.Text = "New User";
    HP_Login.Text = "login";
}
else
{
    HP_User.Text = "welcome ." + Session["UserID"].ToString() ;
    HP_Out.Visible = true;
    HP_Login.Visible = false;
}

Since your login page is a separate page from the page you're talking about, it won't be considered postback when the user is directed back to your page after logging in. So each time your user visits this page, their Session["UserID"] is being set back to "" . 由于您的登录页面与您正在谈论的页面是一个单独的页面,因此当用户登录后被定向回到您的页面时,它将不被视为回发。因此,每次用户访问该页面时,他们的Session["UserID"]设置回"" Try just: 尝试一下:

if (!String.IsNullOrEmpty(Session["UserID"]))
{
    HP_User.Text = "New User";
    HP_Login.Text = "login";
}
else
{
    HP_User.Text = "welcome ." + Session["UserID"].ToString() ;
    HP_Out.Visible = true;
    HP_Login.Visible = false;
}

Try this: 尝试这个:

if (!Page.IsPostBack)
{
    Session["UserID"] = "";

if (Session["UserID"] == "")
{
    HP_User.Text = "New User";
    HP_Login.Text = "login";
}
else
{
    HP_User.Text = "welcome ." + Session["UserID"].ToString() ;
    HP_Out.Visible = true;
    HP_Login.Visible = false;
}
}

Regards 问候

Don't know if you are still needing this or not but would it not help to just check if the Session is empty or not ie 不知道您是否仍需要此功能,但是仅检查会话是否为空也无济于事,即

if (!Page.IsPostBack) 
{    
 if (String.IsNullOrEmpty(Session["UserID"]))
{
 Session["UserID"] = ""; 
}
} 

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

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