Search code examples
asp.netterminologyparse-error

How to set ASP.NET label text to current user name


I am trying to set a user name to a label, but not sure if this is the right syntax -
adding following markup generates a parse error

<asp:Label ID="userNameLabel" runat="server"
     Text='<%= User.Identity.Name.Split(new char[]{'\\'})[1] %>' />

The main problem here is that, I do not know what <%= %> or <%# %> are called, thus cannot Google/Bing.

Can someone point me to a right direction?


Solution

  • Personally I would set the text of the label in the code behind in Page_Load

    userNameLabel.Text = User.Identity.Name.Split('\\')[1];
    

    You will need to ensure that there is a \ in the username or you will get an error.