本文介绍了ASP.NET 中继器交替行高亮显示,但没有完整的 <alternatingitemtemplate/>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 <itemtemplate/> 中的交替行上简单地将一个 css 类添加到一个 div 中,而不需要包含一个完整的 <交替项目模板/> 这将迫使我在未来保持很多标记同步.

I'm trying to accomplish simply adding a css class to a div on alternate rows in my <itemtemplate/> without going to the overhead of including a full blown <alternatingitemtemplate/> which will force me to keep a lot of markup in sync in the future.

我见过一个解决方案,例如 http://blog.net-tutorials.com/2009/04/02/how-to-alternate-row-color-with-the-aspnet-repeater-control/ 我很想使用它,但这对我来说仍然闻不出来".

I've seen a solution such as http://blog.net-tutorials.com/2009/04/02/how-to-alternate-row-color-with-the-aspnet-repeater-control/ which I'm tempted to use but this still doesn't "smell" right to me.

有没有其他人有更易于维护和更直接的解决方案?理想情况下,我希望能够执行以下操作:

Has anyone else got a more maintainable and straightforward solution? Ideally I'd like to be able to do something like:

<asp:repeater id="repeaterOptions" runat="server">
        <headertemplate>
            <div class="divtable">
                <h2>Other Options</h2>
        </headertemplate>
        <itemtemplate>
                <div class="item <%# IsAlternatingRow ? "dark" : "light" %>">

但我不知道如何实现 IsAlternatingRow - 即使使用扩展方法.

But I can't figure out how to implement IsAlternatingRow - even with extension methods.

推荐答案

无需管理自己的变量(递增计数器或布尔值);您可以查看内置的 ItemIndex 属性可以被 2 整除,并使用它来设置一个 css 类:

There is no need to manage your own variable (either an incrementing counter or a boolean); you can see if the built-in ItemIndex property is divisible by two, and use that to set a css class:

class="<%# Container.ItemIndex % 2 == 0 ? "" : "alternate" %>"

这样做的好处是完全基于您的 UI 代码(ascx 或 aspx 文件),并且不依赖于 JavaScript.

This has the benefit of being completely based in your UI code (ascx or aspx file), and doesn't rely on JavaScript.

这篇关于ASP.NET 中继器交替行高亮显示,但没有完整的 &lt;alternatingitemtemplate/&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 06:14