简体   繁体   中英

How to use session on masterpage

我在母版页上添加了登录和注销按钮,我想在登录和注册页面上同时隐藏它们,并在其他页面登录后使登录按钮和登录按钮保持可见状态。我该怎么做。

Set Login button visibility to true and Logout button visibility to false, by default, in your aspx page. After successful login (on login button click), assign user name to Session and set Login button visibility to false, Logout button visibility to true like below.

protected void Login_Click(object sender, EventArgs e)
{
   Session["UserName"] = UserName; //Assign user name
   Login.Visible =true;
   Logout.Visible =true;
}

In your master page, page load should have condition to check session is null or not. This is because if your session is lost, you have to let user to login again.

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["UserName"] != null)
    {
      Logout.Visible =true;
      Login.Visible =false;
    }
    else
    {
      Login.Visible =true;
      Logout.Visible =false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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