Cute Bear
Cute Bear

Reputation: 3293

Binding data to label with static text

<asp:Label ID="IDLabel" runat="server" Text='<%# Bind("ID") %>' />

when I call the DataBind() function, ID is displayed as follows:

14

but what if I want to display the ID like this:

ID: 14

this didn't work.

<asp:Label ID="IDLabel" runat="server" Text='ID: ' + '<%# Bind("ID") %>' />

Upvotes: 0

Views: 2215

Answers (4)

Sebastian
Sebastian

Reputation: 8005

Try this one:

<asp:Label ID="IDLabel" runat="server" Text='<%# "ID: " +Eval("ID").ToString() %>' />

You cannot concatenate the values of attributes in XML.

You basically have XML like this:

<element attribute="ID" + "sometext"/>

which is not valid - instead you need to let the preprocessor change the output of the XML so that only the value of the attribute is modified.

Upvotes: 1

Kishore Kumar
Kishore Kumar

Reputation: 12874

<asp:Label ID="IDLabel" runat="server" Text='<%# "ID: " +Eval("ID").ToString() %>' />

Upvotes: 0

Boriss Pavlovs
Boriss Pavlovs

Reputation: 1441

Text='<%# "ID: " +Eval("ID").ToString() %>' 

Upvotes: 3

codingbiz
codingbiz

Reputation: 26386

<asp:Label ID="IDLabel" runat="server" Text='<%# "ID: " + Bind("ID") %>'   />

Or

<asp:Label ID="IDLabel" runat="server" Text='<%# String.Format("ID: {0}", Bind("ID")) %>'   />

Upvotes: 0

Related Questions