我可以对 GroupTemplate 或 ItemTemplate 进行编号吗?
我想使用 GroupTemplate 将项目列表分成组。 然而,我需要每个组按顺序编号,以便我可以链接到它们并实现一些 JS 分页。 我正在绑定到 IEnumerable
这是一些伪代码。 我希望输出如下所示:
<a href="#group1">Go to Group 1<a>
<a href="#group2">Go to Group 2<a>
<a href="#group3">Go to Group 3<a>
<ul id="group1">
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
<ul id="group2">
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
<ul id="group3">
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
Is this easy to do in a ListView, using GroupTemplate and ItemTemplate?
<asp:ListView ID="lv" runat="server" GroupPlaceholderID="groupPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="groupPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<GroupTemplate>
<ul id="<!-- group-n goes here -->">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</ul>
</GroupTemplate>
<ItemTemplate>
<li>Item</li>
</ItemTemplate>
</asp:ListView>
我可以从数据源和基本数学中获取用于执行顶部链接的组数,但如何将 id="groupN" 数字添加到模板中?
I would like to use a GroupTemplate to separate a list of items into groups. However, I need each Group to be numbered sequentially so I can link to them and implement some JS paging. I'm binding to an IEnumerable
Here's some pseudo code. I would like the output to look like this:
<a href="#group1">Go to Group 1<a>
<a href="#group2">Go to Group 2<a>
<a href="#group3">Go to Group 3<a>
<ul id="group1">
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
<ul id="group2">
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
<ul id="group3">
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
Is this easy to do in a ListView, using GroupTemplate and ItemTemplate?
<asp:ListView ID="lv" runat="server" GroupPlaceholderID="groupPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="groupPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<GroupTemplate>
<ul id="<!-- group-n goes here -->">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</ul>
</GroupTemplate>
<ItemTemplate>
<li>Item</li>
</ItemTemplate>
</asp:ListView>
I can get the number of groups to do the links at the top from the Datasource and basic math, but how do I get id="groupN" number into the template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为你可以 DataBind id,所以我可能会使用隐藏字段,让 JQuery 计算它们,或者使用 CssClass。 您可以使用 Container.DataItemIndex 来获取您的号码。
编辑:只需将
ul
更改为 runat="server",ASP.NET 就会以其臭名昭著的 INamingContainer 格式为您生成一个唯一的 id。 这也将包括一个索引,尽管它类似于 lv_ctrl0_group ,并且是一个实现细节。您可以将处理程序挂接到
ul
的 Init 事件,并向其附加一个数字,使其类似于lv_ctrl0_group1
。 我认为您不能很容易地摆脱前置的 INamingContainer 内容,这可能会破坏任何 IPostDataHandler 控件。I don't think you can DataBind the id, so I'd probably either use a hidden field, have JQuery count them up, or use the CssClass. You can use
Container.DataItemIndex
to get your number.Edit: By just changing the
ul
to be runat="server", ASP.NET will generate a unique id for you in it's infamous INamingContainer format. That will include an index as well, though it'll be something likelv_ctrl0_group
, and is an implementation detail.You could hook a handler to the
ul
's Init event and append a number to it, making it something likelv_ctrl0_group1
. I don't think you can get rid of the prepended INamingContainer stuff very easily, and this would probably break any IPostDataHandler controls.在你的 aspx 文件中:
在你的代码后面(假设是 C#):
In your aspx file:
In your code behind (assuming C#):
上面的 Keltex 解决方案只需稍加改动即可工作; 使用 <%= 代替 <%#,
<%# 不起作用,因为 GroupTemplate 不支持数据绑定
The solution from Keltex above would work with a small change; use <%= instead <%#,
<%# wouldnt work because GroupTemplate doesnt support databinding