2012-06-14 5 views
3

드롭 다운 선택시 동적 컨트롤을 생성하는 최근 문제에 직면했습니다. 선택이 변경되면 기존 컨트롤을 제거하고 다른 동적 컨트롤 집합을 생성해야합니다.동적 컨트롤 제거 : 지우기가 작동하지만 제거되지 않음


    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); 
    } 
} 

또는 예를 들어 패널에서 모든 컨트롤을 제거 할 경우 사용할 수 있습니다 :이 컨트롤은 사용자가 컨트롤의 이름을 알고있는 경우

+0

foreach 루프가 실행되고 있습니까? –

답변

0

당신이 사용할 수 ... 테이블에 추가됩니다

foreach(Control control in panel.Controls){  
     panel.Controls.Remove(control); 
    } 

희망 하시겠습니까?

+0

답장을 보내 주셔서 감사합니다.하지만 문제는 테이블에 동적 컨트롤과 정적 컨트롤을 추가하는 것이므로 Clear() 함수를 통해 제거되지만 제거 함수는 제거하지 않습니다. 다음 페이지가로드되기 전에 정적 컨트롤을 유지하려고합니다. – kinshuk4

+0

당신은 동적 컨트롤을 호스팅하는 일부 컨테이너에 동적 컨트롤을 넣고 시도 했습니까? –

관련 문제