开发者

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 )
{
    fore开发者_StackOverflow中文版ach ( 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 );
    }


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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜