views:

261

answers:

4

I'm iterating through my controls on this web page and when a button is pressed to modify a piece of a data, I'm disabling the other controls on the page. Such controls consist of TextBoxes, ListBoxes, and Buttons. All of these controls have the Enable property so I was wondering if there was a way to just cast the control to some kind of universal data type and set its property of enabled.

protected void DisableSQRcontrols( Control Page )
{
    foreach ( Control ctrl in Page.Controls )
        if ( ctrl is TextBox )
          ((TextBox)ctrl).Enabled = false;
        else if ( ctrl is Button )
            ((Button)ctrl).Enabled = false;
        else if ( ctrl is ListBox )
            ((ListBox)ctrl).Enabled = false;
        else if ( ctrl.Controls.Count > 0 )
            DisableSQRcontrols( ctrl );
}

I'd like to change the top to something like

protected void DisableSQRcontrols( Control Page )
    {
        foreach ( Control ctrl in Page.Controls )
            if ( ( ctrl is TextBox ) ||
                 ( ctrl is Button  ) ||
                 ( ctrl is ListBox ) )
              ((UniversalControlCast)ctrl).Enabled = false;
            else if ( ctrl.Controls.Count > 0 )
                DisableSQRcontrols( ctrl );
    }
+5  A: 

Yes, most are inheriting from WebControl, such as:

System.Web.UI.WebControls.BaseDataBoundControl
System.Web.UI.WebControls.BaseDataList
System.Web.UI.WebControls.Button
System.Web.UI.WebControls.Calendar
System.Web.UI.WebControls.CheckBox
System.Web.UI.WebControls.CompositeControl
System.Web.UI.WebControls.DataListItem
System.Web.UI.WebControls.FileUpload
System.Web.UI.WebControls.HyperLink
System.Web.UI.WebControls.Image
System.Web.UI.WebControls.Label
System.Web.UI.WebControls.LinkButton
System.Web.UI.WebControls.LoginName
System.Web.UI.WebControls.Panel
System.Web.UI.WebControls.SiteMapNodeItem
System.Web.UI.WebControls.Table
System.Web.UI.WebControls.TableCell
System.Web.UI.WebControls.TableRow
System.Web.UI.WebControls.TextBox
System.Web.UI.WebControls.ValidationSummary
Lucero
heh, simple enough. Thanks
Justen
A: 

These all inherit from WebControl, which is where the Enabled property comes from. Casting to WebControl should do what you need.

Aaron
A: 

Lucero is right - here's the code.

protected void DisableSQRcontrols( Control Page ) 
    { 
        foreach ( Control ctrl in Page.Controls ) 
            if ( ( ctrl is TextBox ) || 
                 ( ctrl is Button  ) || 
                 ( ctrl is ListBox ) ) 
              ((WebControl)ctrl).Enabled = false; 
            else if ( ctrl.Controls.Count > 0 ) 
                DisableSQRcontrols( ctrl ); 
    } 
aronchick
Why not `if (ctrl is WebControl)`? Or, even better, cast with `as` and check for null.
Steven Sudit
Because I have two controls being displayed by a Master Page. When a button is clicked, I'm essentially disabling any input from the other control while the user adds data into the other control. If I encompass all web controls, then the user can't input into the textbox on the other control or press any buttons pertaining to that txtbox
Justen
I get that you only want to disable these three types, but I don't get why.
Steven Sudit
The way the page is set up, it's easier to do it this way than make a bunch of control IDs excluded from being disabled.
Justen
Could the controls you want to disable be grouped under a shared Panel or something?
Steven Sudit
They are currently, pnlMaster. I'm not sure how to start from or find pnlMaster and make everything disabled. I tried ((Panel)Page.FindControl("pnlMaster")).Enabled = false; but it gives me the "Object reference not set to an instance of an object" error
Justen
The way I remember it, the Panel has a Controls collection where your TextBox's, Buttons and such are located.
Steven Sudit
Well yes, but I don't know how to access that since the code-behind is coming from another control page. You're just able to view both of them because they're both in the masterpage
Justen
A: 

You can make use of OfType linq extension:

protected void DisableSQRControls(Control control)
{
 foreach(var webControl in control.Controls.OfType<WebControl>())
 {
  webControl.Enabled = false;
  DisableSQRControls( webControl );
 }
}
George Polevoy