Search code examples
c#asp.net.netmaster-pages

Welcome message on Master Page after login


I have a message name to be showed on MasterPage when the user is logged in. Right now it is showing just MyAccount. Please see the code and let me know how to achieve that. Tried but couldn't managed in the master page. Please see my code as ref:-

<div id="nav-right">
  <div class="showhide-account">
     <img src="images/user.png" width="13" height="13" alt="">&nbsp;&nbsp;My Account
  </div>
  <div class="nav-divider">|</div>
  <div id="logout">
          <a href="Login.aspx" id="logOut" runat="server">Logout</a>
   </div>
   <div class="clear">&nbsp;</div>
</div>

Solution

  • You can do this:- There are two way to do so:-

    1st Way:-

    I think you have to add lblusername lable in your div.

    <div class="showhide-account">
        <img src="images/user.png" width="13" height="13" alt="">&nbsp;&nbsp;
            <asp:Label ID="lblUserName" runat="server"></asp:Label>
    </div>
    

    Then, set the Text of lblUserName in code behind

    lblUserName.Text = Session["UserName"].ToString();
    

    2nd Way:- Added Property

    public string UserName
            {
                get
                {
                    //return the object from session
                    return (string)Session["UserName"];
                }
    
                set
                {
                    Session["UserName"] = value;
                }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
               //set UserName with UserName from Database value after username &
               //password verification 
               this.UserName = "XYZ User";
            }
    

    and then in .aspx page,

    <div class="showhide-account">
         <img src="images/user.png" width="13" height="13" 
                              alt="">&nbsp;&nbsp;<%=UserName%>
      </div>