2010-08-13 26 views
0

我有點迷失在這裏與ListView的工作方式。ListView ItemCreated回發

當我的ListView被創建時,我會根據結果集中的數據做一些着色。

protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e) 
{ 
    DataRow myRow; 
    DataRowView myRowView; 
    myRowView = (DataRowView)e.Item.DataItem; 
    myRow = myRowView.Row; 
    HtmlTableRow myTR = (HtmlTableRow)e.Item.FindControl("trRow"); 
    HtmlTableCell myTC = (HtmlTableCell)e.Item.FindControl("tdCell"); 

    Label myCB = (Label)e.Item.FindControl("ResultLabel1"); 
    if (myRow["Result"].ToString().Equals("true")) 
     myTC.Style.Value = "background-color:#00FF00;color: #000000;"; 
    else 
     myTC.Style.Value = "background-color:#FF0000;color: #000000;"; 
} 

ListView有一個尋呼機,並導致一個回傳到ItemCreated,我無法再次達到dataRow。

任何建議如何處理這個,所以我可以處理回發?

更新: 我改變了代碼來讀取稱爲ResultLabel1的True/False的實際值,但它返回一個空字符串..認爲這比從DataSet的結果讀取結果要好。任何人都可以看到最新的錯誤?

代碼:

 protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e) 
    { 
     HtmlTableRow myTR = (HtmlTableRow)e.Item.FindControl("trRow"); 
     HtmlTableCell myTC = (HtmlTableCell)e.Item.FindControl("tdCell"); 

     // Retrieve the current item. 
     ListViewItem item = e.Item; 

     // Verify if the item is a data item. 
     if (item.ItemType == ListViewItemType.DataItem) 
     { 
      // Get the Label ResultLabel1 control in the item. 
      Label myCB = (Label)item.FindControl("ResultLabel1"); 


      if (myCB.Text.Equals("True")) 
       myTC.Style.Value = "background-color:#00FF00;color: #000000;"; 
      else 
       myTC.Style.Value = "background-color:#FF0000;color: #000000;"; 

     } 
    } 

ASP.NET:

  <ItemTemplate> 
      <tr id="trRow" runat="server" style=""> 
       <td> 
        <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' /> 
       </td> 
       <td> 
        <asp:Label ID="DateTime_StartLabel" runat="server" 
         Text='<%# Eval("DateTime_Start") %>' /> 
       </td> 
       <td> 
        <asp:Label ID="DateTime_EndLabel" runat="server" 
         Text='<%# Eval("DateTime_End") %>' /> 
       </td> 
       <td> 
        <asp:Label ID="TestCaseNameLabel" runat="server" 
         Text='<%# Eval("TestCaseName") %>' /> 
       </td> 
       <td> 
        <asp:Label ID="ModelNameLabel" runat="server" Text='<%# Eval("ModelName") %>' /> 
       </td> 
       <td id="tdCell" runat="server"> 
        <asp:Label ID="ResultLabel1" runat="server" 
         Text='<%# Eval("Result") %>' /> 
       </td> 
      </tr> 
     </ItemTemplate> 

回答

2

如果你這樣做是着色作爲數據綁定的結果,你可能會更好處理ItemDataBound事件,而不是ItemCreated

+0

嗨,如何從ItemDataBound事件到達當前行?在ItemCreated中,我這樣做: HtmlTableCell myTC =(HtmlTableCell)e.Item.FindControl(「tdCell」); – StefanE 2010-08-13 13:50:12

+0

無論我的解決方案如何,將代碼放在ItemDataBound中時都做得更好。謝謝! – StefanE 2010-08-13 15:09:38