问题
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=""> 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"> </div>
</div>
回答1:
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="">
<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=""> <%=UserName%>
</div>
回答2:
If your are using membership you can put <%= Membership.GetUser().UserName %> whenever you want current username appear. For example like:
<div id="nav-right">
<div class="showhide-account">
<img src="images/user.png" width="13" height="13" alt=""> My Account - <%= Membership.GetUser().UserName %>
</div>
<div class="nav-divider">|</div>
<div id="logout"><a href="Login.aspx" id="logOut" runat="server">Logout</a></div>
<div class="clear"> </div>
</div>
来源:https://stackoverflow.com/questions/27043733/welcome-message-on-master-page-after-login