ASP:DataList的交替行样式 [英] asp:DataList alternating rows style

查看:273
本文介绍了ASP:DataList的交替行样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据列表:

<asp:DataList ID="DataList1" runat="server" DataKeyField="DocumentID" >
    <HeaderTemplate> 
   Name        </td>
   <td>       header 2       </td>
   <td>       header 3       </td>
   <td>       header 4       </td>
   <td>       viewed        </HeaderTemplate>

 <ItemTemplate>    
   <a href="#" id="doc_<%# Eval("DocumentID") %>" class="docPreview">
    <%# Eval("FileName")%>
   </a>
   </td>
   <td><asp:Label ID="Item2Label" runat="server" Text='<%# Eval("Item2")%>' /></td>
   <td><asp:Label ID="Item3Label" runat="server" Text='<%# Eval("Item3")%>' /></td>
   <td><asp:Label ID="Item4Label" runat="server" Text='<%# Eval("Item4")%>' /></td>
   <td>           
   <asp:CheckBox ID="isViewed" runat="server" Text='<%# Eval("DocumentID") %>' CssClass="hiddenText" />           
</ItemTemplate>
</asp:DataList>

这会产生一个有效的HTML表格。

This produces a valid HTML table.

然而,当我尝试设置AlternatingItemStyle-的CssClass或使用AlternatingItemStyle
它只会让而换到首个TD(每个交替行的),那么整行。
我也尝试使用OnItemDataBound和设置在code后面的css类,但它也只是增加了类到TD而不在TR。

However when I try and set AlternatingItemStyle-CssClass or use AlternatingItemStyle it only makes the change to the first td (of each alternating row) rather then the whole row. I've also tried using OnItemDataBound and setting the css class in the code behind, but it also only adds the class to the td rather then the tr.

难道我只是想念使用DataList和有没有解决这个简单的方法是什么?

Am I simply miss using the DataList, and is there a simple way to solve this?

推荐答案

DataList控件呈现一个表格,表格中的每个项目一个TD。您设计模板的方式,你欺骗DataList控件来显示多列。这导致有效的HTML,而不是如在DataList旨在被使用。该控件不分析模板和检查进一步阵的样式应该应用到。

我建议使用,旨在显示多个控制的另一个控制:

DataList renders a table with a single TD for each item. The way you design your templates, you trick the DataList to show multiple columns. This results in valid HTML, but is not as the DataList is intended to be used. The control does not analyze the templates and check for further TDs that the styles should be applied to.
I'd suggest to use another control that is designed to show multiple controls:

  • DataGrid or GridView. A good comparison of the controls is available here.
  • If you need more control over the HTML that is rendered, use a Repeater.

所有的控件支持不同的样式(通过为这可能导致一些重复的标记交替项的单独的模板中继器)交替项。

All of the controls support different styles for alternating items (Repeater through a separate template for the alternating items which might result in some duplicated markup).

这篇关于ASP:DataList的交替行样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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