ASP.NET Repeater Template, Conditional Code for every Nth element(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 个元素的条件代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET 中继器模板,每个第 N 个元素的条件代码
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
