有选择地禁用 WebControl 元素

发布于 2024-10-10 13:16:15 字数 1584 浏览 3 评论 0原文

我有一个带有 PlaceHolder 元素的 ASP.Net MasterPage。 PlaceHolder 的内容可以通过两种模式查看:读写和只读。

为了实现只读,我想禁用占位符内的所有输入。
我决定通过递归循环 PlaceHolder 的控件集合、查找所有继承自 WebControl 的控件并设置 control.Enabled = false; 来实现此目的。

这是我最初写的:

private void DisableControls(Control c)
{
    if (c.GetType().IsSubclassOf(typeof(WebControl)))
    {
        WebControl wc = c as WebControl;
        wc.Enabled = false;
    }

    //Also disable all child controls.
    foreach (Control child in c.Controls)
    {
        DisableControls(child);
    }
}

这工作正常,所有控件都被禁用......但后来要求发生了变化;)
现在,我们要禁用除具有特定 CssClass 的控件之外的所有控件。

所以,我第一次尝试新版本:

private void DisableControls(Control c)
{
    if (c.GetType().IsSubclassOf(typeof(WebControl)))
    {
        WebControl wc = c as WebControl;
        if (!wc.CssClass.ToLower().Contains("someclass"))
            wc.Enabled = false;
    }

    //Also disable all child controls.
    foreach (Control child in c.Controls)
    {
        DisableControls(child);
    }
}

现在我遇到了一个问题。如果我(例如)有一个包含 ,并且我想保持 DropDownList 处于启用状态,那么这就是不工作。

我在面板上调用DisableControls,它被禁用。然后,它循环遍历子级,并在 DropDownList 上调用 DisableControls,并使其保持启用状态(按预期)。但是,由于面板被禁用,因此当页面呈现时,

标记内的所有内容都被禁用!

你能想出办法解决这个问题吗?我考虑过将 c.GetType().IsSubclassOf(typeof(WebControl)) 更改为 c.GetType().IsSubclassOf(typeof(SomeParentClassThatAllInputElementsInheritFrom)),但我找不到合适的东西!

I have an ASP.Net MasterPage with a PlaceHolder element.
The contents of the PlaceHolder can be viewed in two modes: read-write, and read-only.

To implement read only, I wanted to disable all inputs inside the PlaceHolder.
I decided to do this by recursively looping through the controls collection of the PlaceHolder, finding all the ones which inherit from WebControl, and setting control.Enabled = false;.

Here's what I originally wrote:

private void DisableControls(Control c)
{
    if (c.GetType().IsSubclassOf(typeof(WebControl)))
    {
        WebControl wc = c as WebControl;
        wc.Enabled = false;
    }

    //Also disable all child controls.
    foreach (Control child in c.Controls)
    {
        DisableControls(child);
    }
}

This worked fine, and all controls are disabled... But then the requirement changed ;)
NOW, we want to disable all controls except ones which have a certain CssClass.

So, my first attempt at the new version:

private void DisableControls(Control c)
{
    if (c.GetType().IsSubclassOf(typeof(WebControl)))
    {
        WebControl wc = c as WebControl;
        if (!wc.CssClass.ToLower().Contains("someclass"))
            wc.Enabled = false;
    }

    //Also disable all child controls.
    foreach (Control child in c.Controls)
    {
        DisableControls(child);
    }
}

Now I've hit a problem. If I have (for example) an <ASP:Panel> which contains an <ASP:DropDownList>, and I want to keep the DropDownList enabled, then this isn't working.

I call DisableControls on the Panel, and it gets disabled. It then loops through the children, and calls DisableControls on the DropDownList, and leaves it enabled (as intended). However, because the Panel is disabled, when the page renders, everything inside the <div> tag is disabled!

Can you think of a way round this? I've thought about changing c.GetType().IsSubclassOf(typeof(WebControl)) to c.GetType().IsSubclassOf(typeof(SomeParentClassThatAllInputElementsInheritFrom)), but I can't find anything appropriate!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2024-10-17 13:16:15

您只想禁用输入控件,因此您的代码太通用了。做这样的事情:

if (IsInputControl(wc) && !wc.CssClass.ToLower().Contains("someclass"))
            wc.Enabled = false;

并创建函数 IsInputControl:

bool isInputControl(WebControl ctl) {
    if (ctl is TextBox ||
      ctl is DropDownList || 
      ctl is CheckBox ||
      ...) {
      return true;
    } else {
      return false;
    }
}

虽然我不知道 WebControl 的任何通用属性将其标识为输入控件,但类型并不多,所以它不应该是一个太大的问题做这个。

You only want to disable input controls, so your code is too general. Do something like this:

if (IsInputControl(wc) && !wc.CssClass.ToLower().Contains("someclass"))
            wc.Enabled = false;

and create function IsInputControl:

bool isInputControl(WebControl ctl) {
    if (ctl is TextBox ||
      ctl is DropDownList || 
      ctl is CheckBox ||
      ...) {
      return true;
    } else {
      return false;
    }
}

I don't know of any general property of a WebControl that identifies it as an input control though, but there aren't that many types so it shouldn't be too big a deal to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文