LoginName Control to Display Full Name instead of username
Question:
-
How do I modify the text in LoginName Control.
-
I want to display "Full Name" of the currently logged-in User but the LoginName control displays the username (User Id) that user uses to login.
-
I want to display Full Name along with username in LoginName.
What value LoginName Control displays?
MSDN: "Displays the value of the System.Web.UI.Page.User.Identity.Name property."
So by default with FormsAuthentication it will LoginName will display the username that user used to login. And with Windows Authentication the currenlty logged-in Windows UserName.(Assuming Anonymous Identification is OFF and site requires Login)
LoginName.FormatString Property
Yes, FormatString property of LoginName is what you can use to Modify the text that is displayed.
MSDN: "The FormatString property contains a standard text format string that displays the user's name on the Web page. The string "{0}" indicates where in the string the user's name is inserted."
Suppose you are logged-in as "johndoe". Below are some sample values for FormatString and the corresponding OutPut.
FormatString | Output |
---------------------- | -------------- |
"{0}" (i.e default) | johndoe |
Empty i.e. not defined | johndoe |
"Welcome, {0}" | Welcome, johndoe |
"Welcome to my site" | Welcome to my site |
What do we get from that?
So now if you want to display Full Name of the user who is logged-in inside LoginName control then in the page_Load of the page (Could be a MasterPage) you should set the FormatString Property like below:
<
asp:LoginName ID="LoginName1" runat="server" />protected void Page_Load(object sender, EventArgs e){
//this can come from anywhere like session, database string fullName = "ABC XYZ";
LoginName1.FormatString =
"{0}" + " - " + fullName ; //output: johndoe - ABC XYZ
LoginName1.FormatString = fullName;
// output: ABC XYZ}
Yes, thats it. Play around it to display as you want.
Thanks for reading.
References: