Patrick McDonald
Patrick McDonald

Reputation: 65421

How do I determine if a control is enabled on a page?

I have a TextBox in a Panel on an aspx page.

I need to disable a RequiredFieldValidator if the textBox is not enabled.

If the Panel is disabled, and TextBox.Enabled is True, then the textbox is disabled on the page (which is fine.)

So how can I find out if the TextBox is disabled on the page if the Enabled Property is not reliable?

Note that I need a generic solution, as there may be many nested levels of containers, and the containers are not always Panels.

Upvotes: 2

Views: 197

Answers (2)

albertein
albertein

Reputation: 27118

You can do a recursive search across the controls hierarchy, your control is Enabled if it's Enabled and all their ancestors are Enabled too.

bool IsControlEnabled (WebControl control)
{
    if (!(control.Parent is WebControl)) 
        return control.Enabled;

    return control.Enabled && 
        IsControlEnabled(control.Parent as WebControl);
}

Upvotes: 4

John Rasch
John Rasch

Reputation: 63445

How are you disabling the container controls? Is there a reason you can't disable the Textbox and RequiredFieldValidator controls when you disable their container?

Upvotes: 1

Related Questions