我遇到了最近的一个问题,我正在生成关于下拉选择的动态控制。当选择改变时,我必须生成另一组动态控件,删除现有的控件。删除动态控件:清除正在工作,但不能删除
所以我在做这工作不如下:
private void ClearDynamicControls()
{
if (adapter != null)
{
//This has all the controls saved in some dictionary, key as control ID
var controls = adapter.GetAllControls().Keys;
Control mainControl = (PlaceHolder)this.Form.FindControl("MainContent");
foreach (String controlName in controls)
{
Control controlToRemove = this.Form.FindControl("MainContent").FindControl(controlName);
mainControl.Controls.Remove(controlToRemove);
}
var controls2 = mainControl.Controls;
//clearing the controls in the dictionary
adapter.ClearAllControls();
}
}
但随着清除类似的代码()方法是工作的罚款。那我该怎么办呢?
private void ClearDynamicControls()
{
if (adapter != null)
{
//This has all the controls saved in some dictionary, key as control ID
var controls = adapter.GetAllControls().Keys;
Control mainControl = (PlaceHolder)this.Form.FindControl("MainContent");
mainControl.Controls.Clear();
//clearing the controls in the dictionary
adapter.ClearAllControls();
}
}
通过此代码,所有控件(包括动态和静态)都被删除。那么应该做些什么呢?
请让我知道如果我做错了什么。
我在下拉选择更改事件触发时调用此方法。这些控件添加到表...
你确定foreach循环正在执行吗? –