简体   繁体   中英

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") %>' />
Text='<%# "ID: " +Eval("ID").ToString() %>' 

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.

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

要么

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM