【问题标题】:.NET Itemtemplate repeater?.NET Itemtemplate 中继器?
【发布时间】:2011-07-27 13:18:16
【问题描述】:

我正在开发一个内置于 .net 的网站

以下代码

<asp:Repeater ID="RT_FAQ" runat="server">
<ItemTemplate>
<div class="faq-row" style="z-index: 976;">
   <span class="itp-title"><a href="#" onclick="showhide('div1');"><%# DataBinder.Eval(Container.DataItem, "Question")%></a></span>
    <div id="div1" style="display: none;"><%# DataBinder.Eval(Container.DataItem, "Answer")%></div>
</div>
</ItemTemplate>
</asp:Repeater>

输出一些...

<div class="faq-row" style="z-index: 976;">
    <span class="itp-title">Title</span>
    <div id="div1" style="display: none;">Div contents</div>
</div>
<div class="faq-row" style="z-index: 976;">
    <span class="itp-title">Title</span>
    <div id="div1" style="display: none;">Div contents</div>
</div>
<div class="faq-row" style="z-index: 976;">
    <span class="itp-title">Title</span>
    <div id="div1" style="display: none;">Div contents</div>
</div>
<div class="faq-row" style="z-index: 976;">
    <span class="itp-title">Title</span>
    <div id="div1" style="display: none;">Div contents</div>
</div>

如果你看到所有的 div 都被赋予了 id="div1"。我需要以某种方式给他们所有唯一的 ID,所以第一个是 div1,第二个是 div2,依此类推。

.net 可以吗?

【问题讨论】:

  • 为什么要给他们所有唯一的 ID?你打算以某种方式引用它们吗?
  • 我正在编写一个 JS 脚本,该脚本在单击 span itp-title 时显示 div,因此它们需要是唯一的...
  • leandre_b 有下面的答案,但是根据您的评论,您也可以尝试使用 jQuery 来切换与单击的 span 同级的 div 的可见性。

标签: c# javascript .net css


【解决方案1】:

使用 ItemIndex :

<div id="div<%# Container.ItemIndex %>" style="display: none;"><%# DataBinder.Eval(Container.DataItem, "Answer")%></div>

如果您想从1 开始而不是0,只需添加+ 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多