[英]Removing dynamic controls : Clear is working but not remove
I faced a recent problem, where I was generating the dynamic control on the selection of drop down. 我遇到了一个最近的问题,我在选择下拉菜单时生成动态控制。 When the selection changes, I have to generate another set of dynamic controls, removing the existing controls.
当选择发生变化时,我必须生成另一组动态控件,删除现有控件。
So I was doing following which is not working: 所以我做了以下无效工作:
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();
}
}
But the similar code with Clear() method is working fine. 但是使用Clear()方法的类似代码工作正常。 So what shall I do about it?
那我该怎么办呢?
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();
}
}
By this code, all the controls(both dynamic and static) are removed. 通过此代码,将删除所有控件(动态和静态)。 So what shall be done about it?
那么应该怎么做呢?
Please let me know if I am doing something wrong. 如果我做错了,请告诉我。
I am calling this method on dropdown selection change event firing. 我在下拉选择更改事件触发时调用此方法。 These controls are added to the table...
这些控件被添加到表格中......
If you know your control's names you could use this: 如果您知道控件的名称,可以使用:
foreach(Control control in Controls){
if(control.Name == "yourControlName"){
Controls.Remove(control);
}
}
or if you want to remove all controls from a panel for example you could use: 或者如果你想从面板中删除所有控件,例如你可以使用:
foreach(Control control in panel.Controls){
panel.Controls.Remove(control);
}
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.