0

我想在特定条件下更改行的背景颜色。我的代码是

<asp:ListView ID="lst_SentItems" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_SentItems_ItemDataBound">
                                <LayoutTemplate>
                                    <table cellspacing="0">
                                        <tr class="hdrRowColor1">
                                            <td align="left" width="180px">
                                                EmpName
                                            </td>
                                            <td align="left" width="180px">
                                                Salary
                                            </td>
                                            <td align="left" width="180px">
                                                Address
                                            </td>
                                            <td align="left" width="180px">
                                                Department
                                            </td>
                                          </tr>
                                        <tr id="tr" runat="server">
                                        </tr>
                                    </table>
                                </LayoutTemplate>
                                <ItemTemplate>
                                    <tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "EvenRowColor" : "OddRowColor" %>'>
                                            <td align="left">
                                                <asp:label id="lbl3" runat="server" text='<%# Eval("EmpName")%>' />

                                            </td>
                                            <td align="left">

' />

                                            </td>
                                            <td align="left">
                                               <asp:label id="lbl1" runat="server" text='<%# Eval("Address")%>' />

                                            </td>
                                            <td align="left">
                                               <asp:label id="lbl" runat="server" text='<%# Eval("Department")%>' /> 
                                            </td>

                                    </tr>
                                </ItemTemplate>
                            </asp:ListView>

现在我想给特定部门颜色,比如如果人来自帐户部门,那么行的背景颜色应该是红色,如果人属于 IT 部门,那么背景颜色应该是绿色。我已经在 ItemdataBound 中尝试了一些代码,但是该代码仅更改标签的背景。我想更改行背景。该代码是

 protected void lst_SentItems_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label lblCount = (Label)e.Item.FindControl("lbl");
        if (lblCount != null)
        {
            if (lblCount.Text == IT2")
            {
                           lblCount.BackColor = System.Drawing.Color.Red;
            }
        }
    }
}
4

1 回答 1

2

视觉检查:这只是一个想法,并根据您的要求进行编辑。

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        ListViewDataItem dataitem = (ListViewDataItem)e.Item;
        int policyid = (int)DataBinder.Eval(dataitem.DataItem, "EmpID");
        if (policyid == 3)
        {
            HtmlTableRow cell = (HtmlTableRow)e.Item.FindControl("MainTableRow");

            cell.Style.Add("background-color", "Red");
        }
    }
}
于 2012-05-29T05:49:27.630 回答