asp:DataList alternating rows style

假如想象 提交于 2019-12-11 20:25:12

问题


I have the following 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>

This produces a valid HTML table.

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.

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


回答1:


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).



来源:https://stackoverflow.com/questions/20080797/aspdatalist-alternating-rows-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!