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

前端 未结 4 1595
有刺的猬
有刺的猬 2020-12-31 05:51

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:

相关标签:
4条回答
  • 2020-12-31 06:46

    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.

    0 讨论(0)
  • 2020-12-31 06:46

    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.

    0 讨论(0)
  • 2020-12-31 06:46

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

    0 讨论(0)
  • 2020-12-31 06:47

    You could use the following:

    CommandArgument='<%#String.Format("{0}|{1}", Eval("ArgZero"), Eval("ArgOn"))%>'
    
    0 讨论(0)
提交回复
热议问题