ASP.NET 中继器模板,每个第 N 个元素的条件代码 [英] ASP.NET Repeater Template, Conditional Code for every Nth element

查看:18
本文介绍了ASP.NET 中继器模板,每个第 N 个元素的条件代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 asp.net 转发器来创建一堆图像.图像标记完全相同,因此标准 没问题.

I'm using an asp.net repeater to create a bunch of images. The image markup is all the same so the standard <ItemTemplate> is fine.

但是,我想将 K 个图像包装在一个 div 中.假设我将 25 个以上的图像绑定到中继器,并且每个 div 需要 5 个图像.如何有条件地为 div 创建开始和结束标记?

However, I want to wrap K images in a div. Lets say I bind 25+ images to the repeater and I want 5 images per div. How do I go about conditionally creating the start and close tags for the div?

这种情况是否更适合 for 循环.

Is this a case better suited for a for loop.

推荐答案

这应该对你有用,后面的代码中不需要任何东西(除了绑定转发器..):

This should work for you, with no need for anything in the code behind (other than binding the repeater..):

<asp:Repeater ID="repImages" runat="server">
<HeaderTemplate><div></HeaderTemplate>

<ItemTemplate>
<%# (Container.ItemIndex != 0 && Container.ItemIndex % 5 == 0) ? @"</div><div>" : string.Empty %>
<asp:Image ID="imgGallery" runat="server" ImageUrl='<%# /* your code  here */ %>' />
</ItemTemplate>

<FooterTemplate></div></FooterTemplate>
</asp:Repeater>

这篇关于ASP.NET 中继器模板,每个第 N 个元素的条件代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆