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 个元素的条件代码
基础教程推荐
- C# 9 新特性——record的相关总结 2023-04-03
- 如果条件可以为空 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 将数据集转换为列表 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
