2013-07-25 22 views
3

我正在開發一個SharePoint 2010非可視WebPart,它在大表中顯示某種數據。 應該通過從屬於WebPart的一部分的DropDownList中選擇過濾條件來過濾表格行。Controls.Remove()在CreateChildControls之後不起作用?

DropDownList的OnSelectedIndexChanged事件在CreateChildControls之後和OnPreRender之前觸發。由於表格的單元格包含連接了OnClick事件的LinkBut​​tons,因此必須在CreateChildControls中創建它們才能觸發OnClick事件。

我不知道表中哪些行要隱藏,直到DropDownList的OnSelectedIndexChanged被觸發,所以我在CreateChldControls中創建所有可能的錶行,並嘗試直接在OnSelectedIndexChanged事件中或在OnPreRender中刪除過濾的行。這些行從父表的控件集合中被物理地移除,但是它們被顯示爲nevertheles。

作爲一個測試,我嘗試在創建CreateChildControls方法後刪除一些隨機行,它的工作原理和行不被渲染。

如何我刪除這些行:

Table mt = FindControl("matrixtable") as Table; 

    Helpers.Log("Controls in Table: " + mt.Controls.Count); 
    foreach (int kdid in kdIdsInvisible) 
    { 
    TableRow c = mt.FindControl("kdrow" + kdid) as TableRow; 
    Helpers.Log(c.ID); 
    mt.Controls.Remove(c); 
    } 
    Helpers.Log("Controls in Table: " + mt.Controls.Count); 

輸出:

Controls in Table: 88 
Controls in Table: 2 

但所有行仍呈現...

是否有一個解決方案?先謝謝你!

+0

刪除行你確定你是不是你刪除的控件數據綁定後的工作例如更換線mt.Controls.Remove(c);?嘗試在PreRender上執行刪除操作。 – Peter

+0

我試圖先刪除OnPreRender中的控件。它不起作用。控件被刪除,(我檢查過),但它們仍然呈現。刪除控件只能在CreateChildControls中使用,但是目前我不知道要刪除哪些控件。我會在OnPreRender中知道這一點。雞或蛋的困境。 – elsni

+0

而不是在CreateChildControls上添加表,將項目添加到一個單獨的列表。然後在OnPreRender中添加你知道哪些要添加的。只是一個解決方法。它會注意到代碼有其他問題,如果您不在init或load上添加動態控件,則在處理回發事件時會遇到問題。也許你可以在列表中存儲列表,以在那裏做正確的渲染。 – Peter

回答

2

調試:

循環並寫入屏幕所有行的所有ID的ID。

然後再次循環,並向屏幕寫入所有刪除的行ID。

查找未刪除的兩行,並查看它們是否有ID。可能有拆分表格單元格或其他內容。

最糟糕的情況下,在Visual Studio中調試WP並觀察逐行刪除的行,並觀察表的數量以查看哪個被跳過。嘗試刪除即時窗口的那些行,並查看您得到的錯誤。

1

我認爲你需要從表格行集合中刪除該行。試着用mt.Rows.Remove(c);

這裏是在SelectedIndexChanged event

[ToolboxItemAttribute(false)] 
public class TableTest : WebPart 
{ 
    protected override void CreateChildControls() 
    { 
     // Build a table 
     Table t = new Table(); 
     t.ID = "table"; 
     for (int i = 0; i < 11; i++) 
     { 
      TableRow tr = new TableRow(); 
      t.Rows.Add(tr); 
      for (int j = 0; j < 5; j++) 
      { 
       TableCell tc = new TableCell(); 
       tc.Controls.Add(new LiteralControl("Row " + i + " Cell " + j)); 
       tr.Cells.Add(tc); 
      } 
     } 
     // Add a dropdown 
     DropDownList dl = new DropDownList(); 
     dl.AutoPostBack = true; 
     dl.Items.Add(new ListItem { Text = "Odd", Value = "1" }); 
     dl.Items.Add(new ListItem { Text = "Even", Value = "2" }); 
     dl.SelectedIndexChanged += dl_SelectedIndexChanged; 

     // Add to the controls collection 
     Controls.Add(dl); 
     Controls.Add(t); 
    } 

    void dl_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     // Get the table and dropdown 
     Table t = FindControl("table") as Table; 
     DropDownList dl = sender as DropDownList; 
     if (t != null && dl != null) 
     { 
      int i = 1; 
      // Set up a list to hold the rows to remove 
      IList<TableRow> removeRows = new List<TableRow>(); 

      if (dl.SelectedValue == "1") // Get all odd rows 
      {     
       foreach (TableRow tr in t.Rows) 
       { 
        if (i % 2 == 0) 
        { 
         removeRows.Add(tr); // Add odd rows to the list of rows to remove 
        } 
        i++; 
       } 
      } 
      else // Get all even rows 
      { 
       foreach (TableRow tr in t.Rows) 
       { 
        if (i % 2 == 1) 
        { 
         removeRows.Add(tr); // Add even rows to the list of rows to remove 
        } 
        i++; 
       } 
      } 

      foreach (var tr in removeRows) 
      { 
       t.Rows.Remove(tr); // Remove the rows from the table 
      } 
     } 
    } 
} 
相關問題