ASP.NET 페이지에서 모든 컨트롤을 비활성화하려면 어떻게 하나요?

페이지에 여러 개의 드롭다운 목록이 있는데 사용자가 모두 사용 안 함이라고 표시된 확인란을 선택하면 모두 사용 안 함으로 설정하고 싶습니다. 지금까지이 코드가 있지만 작동하지 않습니다. 어떤 제안이 있나요?

foreach (Control c in this.Page.Controls)
{
    if (c is DropDownList)
        ((DropDownList)(c)).Enabled = false;
}
해결책

각 컨트롤에는 자식 컨트롤이 있으므로 모든 컨트롤에 도달하려면 재귀를 사용해야 합니다:

protected void DisableControls(Control parent, bool State) {
    foreach(Control c in parent.Controls) {
        if (c is DropDownList) {
            ((DropDownList)(c)).Enabled = State;
        }

        DisableControls(c, State);
    }
}

그런 다음 다음과 같이 호출합니다:

protected void Event_Name(...) {
    DisableControls(Page,false); // use whatever top-most control has all the dropdowns or just the page control
} // divs, tables etc. can be called through adding runat="server" property
해설 (1)

이전 게시물 그러나 이것은 어떻게 다만 이것을 해결 문제입니다. 에 따라 제목"지 기능을 해제하려면 어떻게 해야 합 모든 컨트롤 ASP.NET 페이지?"내가 사용하는 반사 이를 달성하기 위해,그것이 모든 작업 통제 유형이 있는 사용을 제공합니다. 단순히 부르 DisableControls 전달에서 상위 제어(I.e., 형식).

C#:

private void DisableControls(System.Web.UI.Control control)
{
    foreach (System.Web.UI.Control c in control.Controls) 
    {
        // Get the Enabled property by reflection.
        Type type = c.GetType();
        PropertyInfo prop = type.GetProperty("Enabled");

        // Set it to False to disable the control.
        if (prop != null) 
        {
            prop.SetValue(c, false, null);
        }

        // Recurse into child controls.
        if (c.Controls.Count > 0) 
        {
            this.DisableControls(c);
        }
    }
}

VB:

    Private Sub DisableControls(control As System.Web.UI.Control)

        For Each c As System.Web.UI.Control In control.Controls

            ' Get the Enabled property by reflection.
            Dim type As Type = c.GetType
            Dim prop As PropertyInfo = type.GetProperty("Enabled")

            ' Set it to False to disable the control.
            If Not prop Is Nothing Then
                prop.SetValue(c, False, Nothing)
            End If

            ' Recurse into child controls.
            If c.Controls.Count > 0 Then
                Me.DisableControls(c)
            End If

        Next

    End Sub
해설 (2)

비활성화하려는 모든 컨트롤을 패널에 넣은 다음 해당 패널을 활성화/비활성화하면 가장 쉽습니다.

해설 (0)

어판의 주위에 페이지의 일부는 당신이 원하는 장애인:

   < asp:Panel ID="pnlPage" runat="server" >
      ...
   < /asp:Panel >

내부의 Page_Load:

   If Not Me.Page.IsPostBack Then
      Me.pnlPage.Enabled = False
   End If

...또는 C#같습니다. :o)

해설 (0)

나와 함께 일하는 ASP.Net HTML 컨트롤 나는 다음과 같이

public void DisableForm(ControlCollection ctrls)
    {
        foreach (Control ctrl in ctrls)
        {
            if (ctrl is TextBox)
                ((TextBox)ctrl).Enabled = false;
            if (ctrl is Button)
                ((Button)ctrl).Enabled = false;
            else if (ctrl is DropDownList)
                ((DropDownList)ctrl).Enabled = false;
            else if (ctrl is CheckBox)
                ((CheckBox)ctrl).Enabled = false;
            else if (ctrl is RadioButton)
                ((RadioButton)ctrl).Enabled = false;
            else if (ctrl is HtmlInputButton)
                ((HtmlInputButton)ctrl).Disabled = true;
            else if (ctrl is HtmlInputText)
                ((HtmlInputText)ctrl).Disabled = true;
            else if (ctrl is HtmlSelect)
                ((HtmlSelect)ctrl).Disabled = true;
            else if (ctrl is HtmlInputCheckBox)
                ((HtmlInputCheckBox)ctrl).Disabled = true;
            else if (ctrl is HtmlInputRadioButton)
                ((HtmlInputRadioButton)ctrl).Disabled = true;

            DisableForm(ctrl.Controls);
        }
    }

라이

DisableForm(Page.Controls);
해설 (0)
  private void ControlStateSwitch(bool state)
{
    foreach (var x in from Control c in Page.Controls from Control x in c.Controls select x)
        if (ctrl is ASPxTextBox)

            ((ASPxTextBox)x).Enabled = status;

        else if (x is ASPxDateEdit)

            ((ASPxDateEdit)x).Enabled = status;
}

내가 사용하는 linq 산도와 경내에 마련된 회장. 을 사용하는 동안 포함해야 합니다 devExpress DevExpress.웹.ASPxEditors lib.

해설 (0)

여기's VB.NET 는 또한 선택 매개 변수를 사용할 수 있도록 활성화를 위한 컨트롤을 뿐입니다.

Private Sub SetControls(ByVal parentControl 으로 제어,선택적인 ByVal enable As Boolean=False)

    For Each c As Control In parentControl.Controls
        If TypeOf (c) Is CheckBox Then
            CType(c, CheckBox).Enabled = enable
        ElseIf TypeOf (c) Is RadioButtonList Then
            CType(c, RadioButtonList).Enabled = enable
        End If
        SetControls(c)
    Next

End Sub
해설 (0)

이 작업을 재귀적으로 수행해야 합니다. 즉, 에 대한 컨트롤의 하위 컨트롤을 비활성화해야 합니다:

protected void Page_Load(object sender, EventArgs e)
{
  DisableChilds(this.Page);
}

private void DisableChilds(Control ctrl)
{
   foreach (Control c in ctrl.Controls)
   {
      DisableChilds(c);
      if (c is DropDownList)
      {
           ((DropDownList)(c)).Enabled = false;
      }
    }
}
해설 (0)

당신이 정말로 원하는 경우 사용**모든***컨트롤이 페이지에서 다음 작업을 수행하는 가장 쉬운 방법은 이것을 설정하는 것입 양식's 는 장애인용 시설을 사실이다.

ASPX:



      ...

코드김:

protected void Page_Load(object sender, EventArgs e)
{
    form1.Disabled = true;
}

하지만 물론,이 것 또한 사용의 체크박스,그래서 당신이't 할 수 있는 확인란을 클릭하여 다시 사용하도록 설정을 제어합니다.

해설 (0)