Justen
Justen

Reputation: 4869

Instead of making an if() for each control type, is there a cast that will allow me to dynamically set the type of control?

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

Upvotes: 2

Views: 2302

Answers (4)

George Polevoy
George Polevoy

Reputation: 7671

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

Upvotes: 1

aronchick
aronchick

Reputation: 7128

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

Upvotes: 0

Aaron
Aaron

Reputation: 7098

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

Upvotes: 0

Lucero
Lucero

Reputation: 60190

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

Upvotes: 5

Related Questions