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:

 

Published Tuesday, June 16, 2009 5:38 PM by guru_sarkar

Comments

# re: LoginName Control to Display Full Name instead of username

Wednesday, June 02, 2010 2:31 AM by soumya

hi guru,

Thanks a lot !! your post has helped me solve my problem in modifying the display format for loginname control :)

# re: LoginName Control to Display Full Name instead of username

Tuesday, June 22, 2010 12:30 PM by marvin

... very nice howto.

how did you call the fullname?

thx very much

# re: LoginName Control to Display Full Name instead of username

Wednesday, June 23, 2010 12:49 PM by guru_sarkar

marvin,

You will need to get the fullname from where you stored it...In example above I have hard-coded as "ABC XYZ".

# re: LoginName Control to Display Full Name instead of username

Tuesday, July 27, 2010 12:06 PM by Reza

To get the Fullname from the database, I need to get the UserId of the logged in user. Now If I place this code in the master page's Page_load event, I get error. System does not find the UserId because the user is not logged in yet.

Can u seggest me anything??  

# re: LoginName Control to Display Full Name instead of username

Tuesday, July 27, 2010 5:04 PM by guru_sarkar

Reza,

You can put your code that needs UserId into the if block like:

if(Request.IsAuthenticated){

//Then get userID and get fullname

}

# re: LoginName Control to Display Full Name instead of username

Thursday, March 24, 2011 2:46 AM by agiles

great explnation to how to display Full Name instead of username

Leave a Comment

(required) 
(required) 
(optional)
(required)