我在这个网页上迭代我的控件,当按下按钮修改一个数据时,我正在禁用页面上的其他控件。此类控件由TextBoxes,ListBoxes和Buttons组成。所有这些控件都有Enable属性,所以我想知道是否有办法将控件转换为某种通用数据类型并将其属性设置为启用。
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 );
}
我想将顶部更改为
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 );
}
答案 0 :(得分:5)
是的,大多数都是继承自WebControl
,例如:
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
答案 1 :(得分:1)
您可以使用OfType linq扩展名:
protected void DisableSQRControls(Control control)
{
foreach(var webControl in control.Controls.OfType<WebControl>())
{
webControl.Enabled = false;
DisableSQRControls( webControl );
}
}
答案 2 :(得分:0)
这些都继承自WebControl
,这是Enabled属性的来源。施放到WebControl
应该做你需要的。
答案 3 :(得分:0)
Lucero是对的 - 这是代码。
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 );
}