无法更改ListView的BackColor

时间:2015-01-12 17:28:38

标签: c# asp.net listview

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

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

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

这是我的ListView配置:

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

这就是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');
    }
}); 

答案 2 :(得分:1)

您可以将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;
 }

希望得到这个帮助。