未应用ListView css类

时间:2014-08-21 12:31:10

标签: html css listview rows alternating

我正在尝试将交替行颜色应用于列表视图。

我在aspx页面的标题中包含了css,以防其他东西可能在引用的样式表中覆盖它。

我在aspx页面上以声明方式编写的其他列表视图中使用了完全相同的方法,并且它们按预期呈现。但是,此列表视图绑定在cs页面中。除了忽略应用于类的任何CSS样式外,它运行完美。

它生成的HTML源代码显示正在将类写入标记。作为第二个选项,我使用了AlternatingTemplate路由,再次将类成功写入HTML代码但仍然没有css渲染。

在asp页面中是否存在关于在cs页面中绑定列表视图以覆盖样式的内容?感谢。

ASPX:

 <style>
 .EvenRowColor{background-color:#FFF;}
 .OddRowColor{background-color: #E9F1F3;}
 </style>

 <asp:ListView ID="lvOtherAccts" runat="server" ItemPlaceholderID=
    "itemPlaceHolder">
            <LayoutTemplate>
                <table id="ConsumptionData">
                    <tr>
                        <th id="Th1" runat="server">
                            Account
                        </th>
                        <th id="Th2" runat="server">
                            Service Address
                        </th>
                        <th id="Th3" runat="server">
                            City
                        </th>
                        <th id="Th4" runat="server">
                            Last Bill Date
                        </th>
                        <th id="Th5" runat="server">
                            Billed Balance Due
                        </th>
                    </tr>
                    <tr ID="itemPlaceholder" runat="server"></tr> 

                </table>
            </LayoutTemplate>

 <ItemTemplate>
                <tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "OddRowColor" : "EvenRowColor" %>'>
                    <td>
                        <asp:Label ID="AcctNbr" runat="server" Text='<%#Eval("acct_nbr")%>' />
                    </td>
                    <td>
                        <asp:Label ID="SvcAddr" align="left" runat="server" Text='<%#Eval("svc_addr")%>' />
                    </td>
                    <td>
                        <asp:Label ID="SvcCity" runat="server" align="left" Text='<%#Eval("svc_city")%>' />
                    </td>
                    <td>
                        <asp:Label ID="BillDate" runat="server" Text='<%#DateTime.Parse(Eval("billing_date").ToString()).ToString("MM/dd/yyyy")%>' />
                    </td>
                    <td style="text-align: right; padding-right: 50px;">
                        <asp:Label ID="Balance" runat="server" Text='<%# Eval("w_bal", "{0:C2}") %>' />
                    </td>
                </tr>
            </ItemTemplate>  
  </asp:ListView>

CS:

 String strConnString10 = WebConfigurationManager.ConnectionStrings["billing_webConnectionString"].ConnectionString;
        SqlConnection con10 = new SqlConnection(strConnString10);
        SqlCommand cmd10 = new SqlCommand("SELECT landlord_nbr, svc_addr, svc_addr2, svc_city, svc_state, svc_zip, acct_nbr, billing_date, w_bal from landlord_info where landlord_nbr='" + ll_num + "'ORDER BY acct_nbr ASC", con10);
        cmd10.Parameters.Add("conn_nbr", SqlDbType.VarChar).Value = Session["LLNum"];
        cmd10.Connection = con10;
        SqlDataAdapter da10 = new SqlDataAdapter(cmd10);
        DataTable dtLLAccts = new DataTable();
        da10.Fill(dtLLAccts);
        lvOtherAccts.DataSource = dtLLAccts;
        lvOtherAccts.DataBind();

HTML输出的片段:

 <tr class='EvenRowColor'>
                    <td>
                        <span id="ContentPlaceHolderMain_lvOtherAccts_AcctNbr_0">209495000</span>
                    </td>
                    <td>
                        <span id="ContentPlaceHolderMain_lvOtherAccts_SvcAddr_0" align="left">266 SEAVER ST                 </span>
                    </td>
                    <td>
                        <span id="ContentPlaceHolderMain_lvOtherAccts_SvcCity_0" align="left">ROXBURY             </span>
                    </td>
                    <td>
                        <span id="ContentPlaceHolderMain_lvOtherAccts_BillDate_0">07/21/2014</span>
                    </td>
                    <td style="text-align: right; padding-right: 50px;">
                        <span id="ContentPlaceHolderMain_lvOtherAccts_Balance_0">$1,925.68</span>
                    </td>
                </tr>

                <tr class='OddRowColor'>
                    <td>
                        <span id="ContentPlaceHolderMain_lvOtherAccts_AcctNbr_1">209642001</span>
                    </td>
                    <td>
                        <span id="ContentPlaceHolderMain_lvOtherAccts_SvcAddr_1" align="left">47 NAZING ST                  </span>
                    </td>
                    <td>
                        <span id="ContentPlaceHolderMain_lvOtherAccts_SvcCity_1" align="left">ROXBURY             </span>
                    </td>
                    <td>
                        <span id="ContentPlaceHolderMain_lvOtherAccts_BillDate_1">07/21/2014</span>
                    </td>
                    <td style="text-align: right; padding-right: 50px;">
                        <span id="ContentPlaceHolderMain_lvOtherAccts_Balance_1">$571.06</span>
                    </td>
                </tr>

1 个答案:

答案 0 :(得分:1)

尝试申请如下。

.EvenRowColor td{background-color:#FFF;}
.OddRowColor td{background-color: #E9F1F3;}

如果上述代码不起作用,则其他一些样式会覆盖您的代码。因此,请使用!important关键字。

.EvenRowColor td{background-color:#FFF !important;}
.OddRowColor td{background-color: #E9F1F3 !important;}