3

我正在开发一个 SharePoint 2010 非可视 WebPart,它在一个大表中显示某种数据。应通过从作为 Web 部件一部分的 DropDownList 中选择过滤条件来过滤表格行。

DropDownList 的 OnSelectedIndexChanged 事件在 CreateChildControls 之后和 OnPreRender 之前触发。因为表格的单元格包含附加了 OnClick 事件的 LinkBut​​tons,所以必须在 CreateChildControls 中创建它们才能触发 OnClick 事件。

在触发 DropDownList 的 OnSelectedIndexChanged 之前,我不知道要隐藏表的哪些行,因此我在 CreateChldControls 中创建所有可能的表行,并尝试稍后在 OnSelectedIndexChanged 事件中直接或在 OnPreRender 中删除过滤的行。这些行已从父表的控件集合中物理删除,但它们始终没有显示。

作为测试,我尝试在创建它们之后在 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

但是所有行仍然被渲染......

有解决方案吗?先感谢您!

4

2 回答 2

2

调试:

循环并将表中所有行的所有 ID 写入屏幕。

然后再次循环,将所有被删除的行 ID 写入屏幕。

找到没有被删除的两行,看看它们是否有 ID。可能有一个拆分表单元格或其他东西。

在最坏的情况下,在 Visual Studio 中调试 WP 并查看一一删除的行并查看表的计数以查看哪些被跳过。尝试删除即时窗口的那些行,看看你得到什么错误。

于 2013-07-30T18:58:52.980 回答
1

我认为您需要从表行集合中删除该行。尝试mt.Controls.Remove(c);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
            }
        }
    }
} 
于 2013-07-31T09:48:37.630 回答