我正在开发一个SharePoint 2010非可视化WebPart,它在一个大表格中显示某种类型的数据。应该通过从作为The部件一部分的DropDownList中选择过滤条件来过滤表格行。
DropDownList的OnSelectedIndexChanged事件在CreateChildControls之后OnPreRender之前激发。因为表的单元格包含附加了OnClick事件的LinkButtons,所以必须在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但所有行仍将呈现...
对此有解决方案吗?提前谢谢你!
发布于 2013-07-31 02:58:53
调试:
循环遍历并将表中所有行的所有ID写入屏幕。
然后再次循环,并将删除的所有行ID写入屏幕。
找到未删除的两行,并查看它们是否有ID。可能有一个拆分的表格单元格或其他什么。
最坏的情况是,在visual studio中调试WP,逐个查看删除的行,并查看表的计数以查看哪些行被跳过。尝试删除“即时”窗口的那些行,并查看得到的错误。
发布于 2013-07-31 17:48:38
我认为您需要从表rows集合中删除行。尝试用mt.Rows.Remove(c);替换mt.Controls.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
}
}
}
} https://stackoverflow.com/questions/17855456
复制相似问题