0

我有一个Repeater控件,它的dataSource 设置了一个类型化的对象列表,并且在内联代码中我想访问它在ItemTemplate标记内的元素属性。我用 eval 表达式尝试了这个,但它不起作用:

<ItemTemplate>
  <tr>
    <td><%# Eval("code") %></td>
    <td><%# Eval("description") %></td>
  </tr>
</ItemTemplate>

有任何想法吗?
谢谢!!

4

2 回答 2

2

您可以使用:<%# DataBinder.Eval(Container.DataItem, "field name") %>

于 2009-04-28T11:57:48.087 回答
0

您的对象是否具有称为“代码”的属性。记住它是区分大小写的。

例如,如果您的对象是...

public class MyObj
{
    public string Code { get; set; }
    public string Description { get; set; }
}

你正在将 a 绑定Collection<MyObj>到你的数据源,

然后你的中继器看起来像......

<asp:repeater id="Repeater1" runat="server">
    <headertemplate>
      <table border="1">
        <tr>
          <td><b>Code</b></td>
          <td><b>Description</b></td>
        </tr>
    </headertemplate>

    <itemtemplate>
      <tr>
        <td> <%# Eval("Code") %> </td>
        <td> <%# Eval("Description") %> </td>
      </tr>
    </itemtemplate>

    <footertemplate>
      </table>
    </footertemplate>
  </asp:repeater>
于 2009-04-28T12:00:21.903 回答