你舉個例子:
ASCX:
<asp:Repeater ID="repItems" runat="server">
<ItemTemplate>
<asp:Panel ID="pnlItem" runat="server">
<asp:LinkButton ID="lnkItem" runat="server" Text='<%# Eval(FieldName) %>' ></asp:LinkButton>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
CS:
public partial class _Default : System.Web.UI.Page
{
public string FieldName = @"Name";
protected void Page_Load(object sender, EventArgs e)
{
List<ArtistInfo> artists = new List<ArtistInfo>();
artists.Add(new ArtistInfo() { Name = "Lady GaGa", Location = "Nebraska" });
artists.Add(new ArtistInfo() { Name = "Justin Timberlake", Location = "Memphis" });
repItems.DataSource = artists;
repItems.DataBind();
}
}
public class ArtistInfo
{
public string Name { get; set; }
public string Location { get; set; }
}
由於<%# %>
只是意味着計算該表達式和數據綁定過程中返回其結果是,你可以把任何你想在那裏。在這種情況下,我們使用FieldName的參數調用Eval,該參數是在代碼隱藏中定義的字符串。