如何防止重复器中的交替模板的 itemtamplate 中的内容重复?
有没有办法防止 itemtemplate 内容重复,而 itemtemplate 内容只会与交替模板块的不同 css 类一起出现?
<asp:Repeater ID="rptCommentHistory" runat="server">
<ItemTemplate>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment").ToString() %>'
Class="itemTemplate"/>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment").ToString() %>'
Class="alternatingTtemTemplate"/>
</AlternatingItemTemplate>
</asp:Repeater>
Is there a way to prevent duplication ot an itemtemplate content which will just appear with a different css class for the alternating template block?
<asp:Repeater ID="rptCommentHistory" runat="server">
<ItemTemplate>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment").ToString() %>'
Class="itemTemplate"/>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment").ToString() %>'
Class="alternatingTtemTemplate"/>
</AlternatingItemTemplate>
</asp:Repeater>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该做你想要的:-)
This should do what you want:-)
我从不使用
AlternatingItemTemplate
。我不喜欢为了拥有替代项而必须复制我的代码,并且我认为如果代码如此不同以至于不能将其归类为重复项,那么您不应该使用Repeater无论如何都要控制。
因此,我总是只使用
ItemTemplate
,并在ItemDataBound
事件中进行所需的任何更改。要确定该项目是正常项目还是交替项目,我会执行以下操作:
在您的情况下,唯一的区别是对
Label
CssClass
的更改,所以我会做类似的事情:I never make use of the
AlternatingItemTemplate
. I don't like having to duplicate my code for the purpose of having an alternating item, and I think that if the code is that different that it cannot be classed as a duplicate, then you shouldn't be using aRepeater
control anyway.Therefore I always just use the
ItemTemplate
, and make any changes I need to in theItemDataBound
event.To determine whether the item is a normal, or alternating item, I would do something like:
In your case the only difference is a change to the
Label
CssClass
, so I would do something like:这并不是真正的重复,不用担心。您正在按预期实施此操作。
我能想到的唯一其他选择是使用 itemtemplate,编写一些在 itemdatabound 事件上触发的代码,并使用 FindControl 以编程方式更改 css 类。
我知道我更愿意使用哪个...
It's not really duplication as such, don't worry about it. You are implementing this pretty much as intended.
The only other option I can think of would be to use the itemtemplate, write some code that is fired on the itemdatabound event and programatically change the css class using FindControl.
I know which I would rather use...
由于似乎建议的解决方案并不令人满意,并且没有其他“.net”解决方案,因此我建议您在 javascript 中执行此操作(因为纯 CSS 中不存在它)。
jQuery 允许您对选择器的偶数和奇数元素应用不同的样式。
它应该与表格行之外的其他元素一起使用...
所以类似这样的东西应该可以工作:
或者您可以将
ItemTemplate
设置为所有元素并使用(它可能会表现更好):As it seems that the proposed solutions are not satisfying and that there are no other ".net" solution, I would suggest that you do that in javascript (as it doesn't exist in pure CSS).
jQuery allows you to apply a different style on even and odds elements of the selector.
It should work with other elements than table rows...
So something like this should work :
Or you can just set
ItemTemplate
to all elements and use (it may perform better) :