如何确定页面上是否启用了控件?

时间:2022-08-11 07:21:15

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

我在aspx页面的Panel中有一个TextBox。

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

如果未启用textBox,我需要禁用RequiredFieldValidator。

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

如果Panel被禁用,TextBox.Enabled为True,则页面上的文本框被禁用(这很好。)

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

那么如果启用属性不可靠,如何在页面上禁用TextBox?

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

请注意,我需要一个通用的解决方案,因为可能有许多嵌套级别的容器,并且容器并不总是Panel。

2 个解决方案

#1


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.

您可以跨控件层次结构进行递归搜索,如果启用了控件,则控件为Enabled,并且所有祖先也都已启用。

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

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

#2


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?

你是如何禁用容器控件的?有没有理由在禁用容器时无法禁用Textbox和RequiredFieldValidator控件?

#1


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.

您可以跨控件层次结构进行递归搜索,如果启用了控件,则控件为Enabled,并且所有祖先也都已启用。

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

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

#2


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?

你是如何禁用容器控件的?有没有理由在禁用容器时无法禁用Textbox和RequiredFieldValidator控件?