【问题标题】:How to dynamically set control IDs inside a repeater template?如何在转发器模板中动态设置控件 ID?
【发布时间】:2010-04-27 00:18:10
【问题描述】:

这是一个令人困惑的问题,我在 StackOverflow 上没有看到一个很好的答案,尽管有几个问题……我有一种情况,我想这样做:

<asp:Repeater ID="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
    <ItemTemplate>
        <li id="id?">
            All the other stuff
        </li>
    </ItemTemplate>
</asp:Repeater>

问题...如何根据它们绑定的 ItemIndex 将我的

  • 元素的 ID 设为 id1、id2、id3 等?到目前为止,我想出的最......呃......“优雅”的解决方案是将
  • 替换为 asp:Literal 并转储
  • ' 文本。但这只是感觉……太不对劲了。不,我没有使用 ASP.NET 4.0,我读过它会提供这个功能。

    【问题讨论】:

      标签: asp.net repeater


      【解决方案1】:

      像这样:

      <asp:Repeater ID="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
          <ItemTemplate>
              <li id="li<%# ((RepeaterItem)Container).ItemIndex + 1%>">
                  All the other stuff
              </li>
          </ItemTemplate>
      </asp:Repeater>
      

      【讨论】:

      • 省略演员表并只使用Container.ItemIndex就足够了。
      • 如果我想这样 ' class='clearbth amc_row'>" : "" %> ?
      • 谢谢,这救了我
      猜你喜欢
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 2011-05-04
      • 2011-02-03
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      相关资源
      最近更新 更多