2012-05-29 33 views
0

我想改变,特别condition.My代码行的背景色是如何根据条件突出显示列表视图中的行?

<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但代码只改变labels.i的回地面要换行回ground.That代码

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

回答

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"); 
     } 
    } 
}