【问题标题】:How to access to the elements properties inside a Repeater?如何访问中继器中的元素属性?
【发布时间】:2010-10-22 07:14:18
【问题描述】:

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

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

有什么想法吗?
谢谢!!

【问题讨论】:

    标签: asp.net list repeater datasource


    【解决方案1】:

    您可以使用:&lt;%# DataBinder.Eval(Container.DataItem, "field name") %&gt;

    【讨论】:

      【解决方案2】:

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

      例如如果你的对象是……

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

      您正在将Collection&lt;MyObj&gt; 绑定到您的数据源,

      那么你的中继器看起来像......

      <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>
      

      【讨论】:

      • 感谢 Eoin,但这是我尝试过的,它给了我一个错误,它只能在数据绑定控件的上下文中使用。
      猜你喜欢
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 2012-10-27
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多