How do you concatenate text when using Bind expression in asp.net

十年热恋 提交于 2019-12-30 02:53:05

问题


What is the syntax to concatenate text into a binding expression for an asp.net webpage (aspx).

For example if I had a hyperlink that was being bound like this:

<asp:HyperLink id="lnkID" NavigateUrl='<%# Bind("Link") %>' Target="_blank" 
                        Text="View" runat="server"/>

How do you change, say, the Text to concatenate a bound value with a string? Variations like this aren't quite right.

Text='<%# Bind("ID") + " View" %>'

neither does

Text='<%# String.Concat(Bind("ID"), " View") %>'

回答1:


Use Eval instead.

Text='<%# Eval("ID", "{0} View") %>'

Eval is also better if the value is not going to be updated, where Bind allows two way data binding.




回答2:


You can also place the "concatenation" in the text portion of a tag if using a template field:

<asp:TemplateField HeaderText="Name" SortExpression="sortName">
<ItemTemplate>
   <asp:LinkButton ID="lbName" runat="server" OnClick="lbName_Click" CommandArgument='<%# Eval("ID") %>'>
         <%--Enter any text / eval bindind you want between the tags--%>
         <%# Eval("Name") %> (<%# Eval("ID") %>)
   </asp:LinkButton>
</ItemTemplate>

This results in output like:

Name (ID)

inside of the template column.




回答3:


I have used String.Format("{0}{1}"... before to good effect.




回答4:


You could use the following:

CommandArgument='<%#String.Format("{0}|{1}", Eval("ArgZero"), Eval("ArgOn"))%>'


来源:https://stackoverflow.com/questions/360851/how-do-you-concatenate-text-when-using-bind-expression-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!