簡體   English   中英

如何禁用ASP.NET頁面中的所有控件?

[英]How do I disable all controls in ASP.NET page?

我在頁面中有多個下拉列表,如果用戶選擇一個禁用全部的復選框,則要禁用所有。 到目前為止,我有這個代碼,它無法正常工作。 有什么建議么?

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

我知道這是一個老帖子,但這就是我剛剛解決了這個問題的方法。 根據標題“我如何禁用ASP.NET頁面中的所有控件?” 我用反射來達到這個目的; 它適用於具有Enabled屬性的所有控件類型。 只需調用傳入父控件(即表單)的DisableControls即可。

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

如果您在面板中放置要禁用的所有控件,然后啟用/禁用面板,這將是最簡單的。

在要禁用的頁面部分周圍放置一個面板:

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

在Page_Load里面:

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

......或C#等價物。 :O)

我正在使用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);
  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 aproach。 使用devExpress時,您必須包含DevExpress.Web.ASPxEditors lib。

你必須遞歸,我的意思是你必須禁用控件的子控件:

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

這是一個VB.NET版本,它也帶有一個可選參數,因此它也可用於啟用控件。

Private Sub SetControls(ByVal parentControl As Control,可選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

如果您確實要禁用頁面上的所有控件,那么最簡單的方法是將表單的Disabled屬性設置為true。

ASPX:

<body>
    <form id="form1" runat="server">
      ...
    </form>
</body>

代碼隱藏:

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

但是,當然,這也會禁用您的復選框,因此您將無法單擊該復選框以重新啟用控件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM