我想在特定条件下更改行的背景颜色。我的代码是
<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">
'/&gt;
</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;
}
}
}
}
答案 0 :(得分: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");
}
}
}