드롭 다운 선택시 동적 컨트롤을 생성하는 최근 문제에 직면했습니다. 선택이 변경되면 기존 컨트롤을 제거하고 다른 동적 컨트롤 집합을 생성해야합니다.동적 컨트롤 제거 : 지우기가 작동하지만 제거되지 않음
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(Control control in Controls){
if(control.Name == "yourControlName"){
Controls.Remove(control);
}
}
또는 예를 들어 패널에서 모든 컨트롤을 제거 할 경우 사용할 수 있습니다 :이 컨트롤은 사용자가 컨트롤의 이름을 알고있는 경우
foreach 루프가 실행되고 있습니까? –