如何在 asp.net 中串接两个绑定?

How to string concatenate two bindings in asp.net?

我想将两个绑定字符串连接成一个字符串。我怎样才能做到这一点?我尝试了下面的方法,但是当我 运行 应用程序时只有 LastName 出现。

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

有几个选项。

  1. 在您的代码中创建组合 属性 behind/model:

    // cs
    public string FullName 
    {
        get { return $"{FirstName} {LastName}"; }   
    }
    
    // aspx
    <asp:Label ID="txtFacultyName" runat="server" Text='<%#Bind("FullName") %>'/>
    
  2. 使用Eval。 Eval 将允许单向绑定,这非常适合在 asp:Label

    中显示
    <asp:Label ID="txtFacultyName" 
               runat="server" 
               Text='<%# string.Format("{0} {1}", Eval("FirstName"), Eval("LastName")) %>'/>