tags:

views:

51

answers:

2

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.

+1  A: 

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?

John Rasch
+3  A: 

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