无法更改列表视图的背景色



我目前正在尝试更改列表视图中行的背景颜色。根据我的研究,我可以使用此代码来更改行颜色:

foreach(ListViewItem lvi in LV_Equipment)
{
    lvi.BackColor = Color.Red;
}

当我尝试使用此代码时,我的 ListViewItem(lvi) 没有 BackColor 属性。我不太确定为什么会这样,从我在网上看到的来看,这是解决这个问题的方法。我的页面上有 5 个列表视图,但所有 5 个都找不到此属性。

这是我的列表视图配置:

<asp:ListView ID="LV_Equipment" runat="server" DataSourceID="SQL_Equipment" InsertItemPosition="LastItem" DataKeyNames="JobUnitKey" OnPreRender="LV_Equipment_PreRender" OnSelectedIndexChanged="LV_Equipment_SelectedIndexChanged" OnItemCanceling="LV_Equipment_ItemCanceling" OnItemDataBound="LV_Equipment_ItemDataBound" OnItemInserting="LV_Equipment_ItemInserting" OnDataBound="LV_Equipment_DataBound" >
                                <AlternatingItemTemplate>
                                    <tr style="background-color:#FFF8DC;">
                                        <td>
                                            <asp:Button ID="SelectButton" runat="server" CommandName="Select" Text="Delete" />
                                            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                                        </td>
                                            <!--<asp:Label ID="JobUnitKeyLabel" runat="server" Text='<%# Eval("JobUnitKey") %>' />-->
                                        <td>
                                            <asp:Label ID="JobIDLabel" runat="server" Text='<%# Eval("JobID") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="DispatchDateLabel" runat="server" Text='<%# Eval("DispatchDate", "{0:MMM dd yyyy}") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="TimeToArriveLabel" runat="server" Text='<%# Eval("TimeToArrive", "{0:HH:mm}") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="UnitIDLabel" runat="server" Text='<%# Eval("UnitID") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="DriverNameLabel" runat="server" Text='<%# Eval("DriverName") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="TruckRateLabel" runat="server" Text='<%# Eval("TruckRate", "{0:$0.00}") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="NotesLabel" runat="server" Text='<%# Eval("Notes") %>' />
                                        </td>
                                    </tr>
                                </AlternatingItemTemplate>
                                <EditItemTemplate>
                                    <tr style="background-color:#008A8C;color: #FFFFFF;">
                                        <td>
                                            <asp:Button ID="UpdateButton" runat="server" CommandName="Cancel" Text="Update" />
                                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                                        </td>
                                        <!--<asp:Label ID="JobUnitKeyLabel" runat="server" Text='<%# Eval("JobUnitKey") %>' />-->
                                        <td>
                                            <asp:Label ID="LBL_Insert_Equipment_JobID" runat="server" Text='<%# Bind("JobID") %>'></asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="DispatchDateTextBox" runat="server" Text='<%# Bind("DispatchDate", "{0:MM-dd-yyyy}") %>' />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="TimeToArriveTextBox" runat="server" Text='<%# Bind("TimeToArrive", "{0:HH:mm}") %>' />
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SQL_GetUnits" DataTextField="UnitID" DataValueField="UnitID" SelectedValue='<%# Bind("UnitID") %>'>
                                            </asp:DropDownList>
                                            <asp:Button ID="BTN_Equipment_Insert_Add_Unit" runat="server" OnClick="BTN_Equipment_Insert_Add_Unit_Click" Text="Add Unit" />
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="DDL_Insert_Drivers" runat="server" DataSourceID="SQL_GetDrivers" DataTextField="DriverName" DataValueField="DriverID" >
                                            </asp:DropDownList>
                                            <asp:Button ID="BTN_Equipment_Insert_Add_Driver" runat="server" OnClick="BTN_Equipment_Insert_Add_Driver_Click" Text="Add Driver" />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="TruckRateTextBox" runat="server" Text='<%# Bind("TruckRate") %>' />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="NotesTextBox" runat="server" Text='<%# Bind("Notes") %>' />
                                        </td>
                                    </tr>
                                </EditItemTemplate>
                                <EmptyDataTemplate>
                                    <table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                                        <tr>
                                            <td>No data was returned.</td>
                                        </tr>
                                    </table>
                                </EmptyDataTemplate>
                                <InsertItemTemplate>
                                    <tr style="">
                                        <td>
                                            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                                        </td>
                                        <td>
                                            <asp:Label ID="LBL_Insert_Equipment_JobID" runat="server" Text='<%# Bind("JobID") %>'></asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="DispatchDateTextBox" runat="server" PlaceHolder="MM-dd-yyyy" Text='<%# Bind("DispatchDate") %>' />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="TimeToArriveTextBox" runat="server" PlaceHolder="HH:mm" Text='<%# Bind("TimeToArrive") %>' />
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SQL_GetUnits" DataTextField="UnitID" DataValueField="UnitID" SelectedValue='<%# Bind("UnitID") %>'>
                                            </asp:DropDownList>
                                            <asp:Button ID="BTN_Equipment_Insert_Add_Unit" runat="server" OnClick="BTN_Equipment_Insert_Add_Unit_Click" Text="Add Unit" />
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="DDL_Insert_Drivers" runat="server" DataSourceID="SQL_GetDrivers" DataTextField="DriverName" DataValueField="DriverID" SelectedValue='<%# Bind("DriverID") %>'>
                                            </asp:DropDownList>
                                            <asp:Button ID="BTN_Equipment_Insert_Add_Driver" runat="server" OnClick="BTN_Equipment_Insert_Add_Driver_Click" Text="Add Driver" />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="TruckRateTextBox" runat="server" Text='<%# Bind("TruckRate") %>' />
                                        </td>
                                        <td>
                                            <asp:TextBox ID="NotesTextBox" runat="server" Text='<%# Bind("Notes") %>' />
                                        </td>
                                    </tr>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <tr style="background-color:#DCDCDC;color: #000000;">
                                        <td>
                                            <asp:Button ID="SelectButton" runat="server" CommandName="Select" Text="Delete" />
                                            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                                        </td>
                                            <!--<asp:Label ID="JobUnitKeyLabel" runat="server" Text='<%# Eval("JobUnitKey") %>' />-->
                                        <td>
                                            <asp:Label ID="JobIDLabel" runat="server" Text='<%# Eval("JobID") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="DispatchDateLabel" runat="server" Text='<%# Eval("DispatchDate", "{0:MMM dd yyyy}") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="TimeToArriveLabel" runat="server" Text='<%# Eval("TimeToArrive", "{0:HH:mm}") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="UnitIDLabel" runat="server" Text='<%# Eval("UnitID") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="DriverNameLabel" runat="server" Text='<%# Eval("DriverName") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="TruckRateLabel" runat="server" Text='<%# Eval("TruckRate", "{0:$0.00}") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="NotesLabel" runat="server" Text='<%# Eval("Notes") %>' />
                                        </td>
                                    </tr>
                                </ItemTemplate>
                                <LayoutTemplate>
                                    <table runat="server">
                                        <tr runat="server">
                                            <td runat="server">
                                                <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                                    <tr runat="server" style="background-color:#DCDCDC;color: #000000;">
                                                        <th runat="server"></th>
                                                        <th runat="server" id="JobUnitKeyRow">JobUnitKey</th>
                                                        <th runat="server">JobID</th>
                                                        <th runat="server">DispatchDate</th>
                                                        <th runat="server">TimeToArrive</th>
                                                        <th runat="server">UnitID</th>
                                                        <th runat="server">DriverName</th>
                                                        <th runat="server">TruckRate</th>
                                                        <th runat="server">Notes</th>
                                                    </tr>
                                                    <tr id="itemPlaceholder" runat="server">
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                        <tr runat="server">
                                            <td runat="server" style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
                                                <asp:DataPager ID="DataPager1" runat="server">
                                                    <Fields>
                                                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                                                        <asp:NumericPagerField />
                                                        <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                                                    </Fields>
                                                </asp:DataPager>
                                            </td>
                                        </tr>
                                    </table>
                                </LayoutTemplate>
                                <SelectedItemTemplate>
                                    <tr style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;">
                                        <td>
                                            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
                                            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                                        </td>
                                        <td>
                                            <asp:Label ID="JobUnitKeyLabel" runat="server" Text='<%# Eval("JobUnitKey") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="JobIDLabel" runat="server" Text='<%# Eval("JobID") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="DispatchDateLabel" runat="server" Text='<%# Eval("DispatchDate") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="TimeToArriveLabel" runat="server" Text='<%# Eval("TimeToArrive") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="UnitIDLabel" runat="server" Text='<%# Eval("UnitID") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="DriverNameLabel" runat="server" Text='<%# Eval("DriverName") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="TruckRateLabel" runat="server" Text='<%# Eval("TruckRate") %>' />
                                        </td>
                                        <td>
                                            <asp:Label ID="NotesLabel" runat="server" Text='<%# Eval("Notes") %>' />
                                        </td>
                                    </tr>
                                </SelectedItemTemplate>
                            </asp:ListView>

这就是CSS的用途。 看起来您的 ItemTemplate 的背景色为 #dcdcdc(浅灰色)。 您可以将内联样式更改为适当的红色阴影,或者(更好的)您可以为模板分配一个 css 类并为该 css 类分配背景(例如 – .bg-red { background:red;})。

响应有条件地更改背景颜色的需要。 如果可能的话,我会使用javascript或jQuery。 在 Web 窗体中,如果影响背景颜色的值因用户输入而更改,这可以防止服务器回发/往返。 一个简单的jQuery块:

$('.itemClass').each(function (item) {
    if (item.value == someVal) {
        item.addClass('bg-red');
    }
}); 

尝试从项模板中的 tr 标记中删除背景色值。

您可以将 paremeter CssClass 添加到您的 ListBox 中:

<asp:ListBox ID="ListBox1" runat="server" CssClass="ListBoxnewStyle"></asp:ListBox>

在您的 CSS 文件中:

1.- 在标题标签中添加对 HTML 网页的引用

<link href="StyleSheet1.css" rel="stylesheet" />

2.- CSS文件必须包含如下内容:

.ListBoxnewStyle 
 {
   background-color:red;
 }

希望这有帮助。

最新更新