ASP.NET中继器交替行高亮显示没有完整的< alternatingitemtemplate /> [英] ASP.NET repeater alternate row highlighting without full blown <alternatingitemtemplate/>

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

问题描述

我试图完成简单的添加一个css类到我的< itemtemplate /> 中的替代行上的div,而不需要包含一个完整的发送< alternatingitemtemplate /> ,这将迫使我将来保持很多标记同步。

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 属性被两个整除,并使用它设置一个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;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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