Richard77
Richard77

Reputation: 21621

How does Control.Enabled works?

I'd like to disable all the controls inside this Listview items. This MSDN page says that Enabled is defined as Control's class property.

foreach (Control ctrl in eItem.Controls)
{
    ctrl.Enabled = false;
}

I'm getting an error "Control does not contain a definition of Enabled...". Only when I cast every control then error goes away.

foreach (Control ctrl in eItem.Controls)
{
    ((TextBox)ctrl).Enabled = false;
}

I have DropDownList, TextBox, Button, CheckBox,.. Is there a fastest and general way of doing that?

Upvotes: 0

Views: 58

Answers (1)

Andrei
Andrei

Reputation: 56688

The link you have provided misled you because it talks about Windows Forms control, not the Web Forms one. In Web Froms property Enabled is defined in WebControl class, which is a subclass of Control and is a superclass of TextBox, DropDownList etc. So here is what you can do:

foreach (Control ctrl in eItem.Controls)
{
    WebControl webCtrl = ctrl as WebControl;
    if (webCtrl != null)
    {
        webCtrl.Enabled = false;
    }
}

Upvotes: 2

Related Questions