views:

415

answers:

4

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?

A: 

If you're trying to convert User.Identity.Name into a couple of strings, it looks changing char[] to string[], should do the trick.

briercan
+3  A: 

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.

RR
For now, this is what I settled with. Thanks RR
Sung Meister
A: 

The <%# %> syntax is for data binding. It will work for what you want to do, you will need to make sure that DataBind() is called.

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

Other options include:

Set the Text property from the Page_Load event.

void Page_Load(object sender, EventArgs e)
{
    userNameLabel.Text = User.Identity.Name.Split('\\')[1];
}

Wrap the label around the write.

<asp:Label ID="userNameLabel" runat="server"><%= User.Identity.Name.Split('\\')[1] %></asp:Label>
Mike J
A: 

This works as well.

    <asp:Label ID="userNameLabel" runat="server">
        <%= User.Identity.Name %>
    </asp:Label>
Sung Meister