Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have multiple dropdownlist in a page and would like to disable all if user selects a checkbox which reads disable all. So far I have this code and it is not working. Any suggestions?

foreach (Control c in this.Page.Controls) {     if (c is DropDownList)         ((DropDownList)(c)).Enabled = false; } 
like image 908
Mohamed Avatar asked Feb 02 '09 23:02

Mohamed


People also ask

How to disable a page in c#?

To do this you can set the @Page directive's ViewStateMode property to Disabled and the control of interest's ViewStateMode property to Enabled. As in the following code: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailPage. aspx.

How do I disable a button in webform?

If you just want to disable it and nothing else, then add attribute Enabled = "false". ImageButton1. Enabled = false; If the form is located in the master page, then you'll have to access that control from the content page via code behind and set the Enabled attribute.


2 Answers

Each control has child controls, so you'd need to use recursion to reach them all:

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

Then call it like so:

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 
like image 73
John Sheehan Avatar answered Oct 28 '22 20:10

John Sheehan


I know this is an old post but this is how I have just solved this problem. AS per the title "How do I disable all controls in ASP.NET page?" I used Reflection to achieve this; it will work on all control types which have an Enabled property. Simply call DisableControls passing in the parent control (I.e., Form).

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 
like image 34
Justin Clarke Avatar answered Oct 28 '22 20:10

Justin Clarke