2011-10-21 3 views
1

내 스펙은 드롭 다운면 활성을 제어 할 수있는 라디오 버튼 그룹에 따라 또는 두 개의 달력 상자 요구, 그래서이 사용자 제어했다 :컨트롤 그룹을 활성화/비활성화하는 방법은 무엇입니까?

<asp:RadioButton 
    GroupName="group1" 
    ID="DateMacroRadioButton" 
    runat="server" 
    Checked="true" /> 
<uc:DateMacroSelector ID="DateMacroSelector1" runat="server" /> 
<asp:RadioButton GroupName="group1" 
    ID="CustomDateRadioButton" 
    runat="server" 
    AutoPostBack="true" Text="" /> 
<uc:DateSelector ID="DateSelector1" runat="server" /> 
to 
<uc:DateSelector ID="DateSelector1" runat="server" /> 

enter image description here

내가 활성화 좋아 줄을/그룹을 사용하여 하위 컨트롤 사용 중지 :

<asp:RadioButton 
    GroupName="group1" 
    ID="DateMacroRadioButton" 
    runat="server" 
    Checked="true" /> 
<uc:EnableGroup ID="EnableGroup1" runat="server" 
    CheckBoxId="DateMacroRadioButton"> 
    <uc:DateMacroSelector ID="DateMacroSelector1" runat="server" /> 
</uc:EnableGroup> 
<asp:RadioButton GroupName="group1" 
    ID="CustomDateRadioButton" 
    runat="server" 
    AutoPostBack="true" Text="" /> 
<uc:EnableGroup ID="EnableGroup12 runat="server" 
    CheckBoxId="CustomDateRadioButton"> 
    <uc:DateSelector ID="DateSelector1" runat="server" /> 
    to 
    <uc:DateSelector ID="DateSelector1" runat="server" /> 
</uc:EnableGroup> 

어떻게 이런 방식으로 작동하는 사용자 정의 컨트롤을 설계 할 수 있습니까?

답변

1

당신은이 같은 작업한다고, 사용자 지정 컨트롤을 만들 수 있습니다 (이 디자이너는 항상 성가신에서 작동하도록하기 위해 "parsechildren"설정을 얻는하지만이 옳다고 생각) :

[ParseChildren(typeof(WebControl), ChildrenAsProperties = true, 
    DefaultProperty = "ChildControls"), NonVisualControl] 
public class EnableGroup: Control 
{ 
     public EnableGroup() { 
      ChildControls = new Collection<WebControl>(); 
     } 

     protected override void OnPreRender(EventArgs e) 
     { 
      foreach (WebControl ctl in Controls) { 
       ctl.Enabled = this.Enabled; 
      } 
      base.OnPreRender(e); 
     } 

     public Collection<WebControl> ChildControls 
     { 
      get; 
      protected set; 
     } 
} 

그러나 솔직히, 확장 메소드 등을 작성하는 것만 큼 쉽습니다.

public static SetChildEnabled(this WebControl parent) { 
    foreach (WebControl ctl in parent.Controls) { 
     ctl.Enabled = parent.Enabled; 
    } 
} 

그럼 그냥 ...

<asp:PlaceHolder runat="server" id="Group1"> 
</asp:PlaceHolder> 

당신의 그룹을 포장 사전 렌더링에 그 코드를 호출하는 오래된 컨트롤을 사용 :

Group1.SetChildEnabled() 

당신이 중 하나를 만들기에 아주 쉽게 반복적으로 작동 할 수 원하는 경우 더 깊게 중첩 된 자식을 처리합니다.

생각해 보니 어쩌면 PlaceHolder을 확장하고 위의 어려운 부분을 건너 뛰는 컨트롤을 만들 수 있습니다 ... 컨테이너에서 허용되는 개체 유형을 제어 할 수 있기를 원한다면 그렇게하는 것이 좋습니다. 그것은, 그러나 과잉일지도 모른다.

+0

이것은 좋은 답변입니다. 감사합니다. 내 생각에 지금은 Controls 컬렉션을 반복하고 Enabled (반복적으로)라는 부울 속성을 반영 할 수 있습니다. –

+0

자녀의 시나리오를 어떻게 지원할 수 있는지 알 수 있습니까? UserControls 컨트롤? (첫 번째 방법 - 내 서버 컨트롤 만들기에 대해 이야기하기) - 모든 항목 유형이 WebControl이어야한다는 오류가 표시됩니다. 그냥 궁금 해서요 - 지금 자리 표시 자 방식에서 파생 된 것을 시도합니다 ... –

+0

확인 그것은 - 웹 컨트롤을 템플릿 컨트롤로 변경하고 "is"를 사용하여 해당 유형이 내가 활성화/비활성화 할 수 있는지 확인합니다. 감사합니다! 나는 이것을 대답 할 것입니다. –

관련 문제