我遇到了最近的一個問題,我正在生成關於下拉選擇的動態控制。當選擇改變時,我必須生成另一組動態控件,刪除現有的控件。刪除動態控件:清除正在工作,但不能刪除
所以我在做這工作不如下:
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循環正在執行嗎? –