【问题标题】:How to Enable/Disable a group of controls?如何启用/禁用一组控件?
【发布时间】:2011-10-22 06:01:57
【问题描述】:

我的规范要求在单选按钮组旁边有一个下拉菜单或两个日历框来控制哪个是活动的,所以我制作了这个用户控件:

<asp:RadioButton 
    GroupName="group1" 
    ID="DateMacroRadioButton" 
    runat="server" 
    Checked="true" />
<uc:DateMacroSelector ID="DateMacroSelector1" runat="server" />
<asp:RadioButton GroupName="group1" 
    ID="CustomDateRadioButton" 
    runat="server" 
    AutoPostBack="true" Text="" />
<uc:DateSelector ID="DateSelector1" runat="server" />
to
<uc:DateSelector ID="DateSelector1" runat="server" />

我想使用组启用/禁用子控件:

<asp:RadioButton 
    GroupName="group1" 
    ID="DateMacroRadioButton" 
    runat="server"  
    Checked="true" />
<uc:EnableGroup ID="EnableGroup1" runat="server"
   CheckBoxId="DateMacroRadioButton">
   <uc:DateMacroSelector ID="DateMacroSelector1" runat="server" />
</uc:EnableGroup>
<asp:RadioButton GroupName="group1" 
    ID="CustomDateRadioButton" 
    runat="server" 
    AutoPostBack="true" Text="" />
<uc:EnableGroup ID="EnableGroup12 runat="server" 
    CheckBoxId="CustomDateRadioButton">
   <uc:DateSelector ID="DateSelector1" runat="server" />
   to
   <uc:DateSelector ID="DateSelector1" runat="server" />
</uc:EnableGroup>

我该如何设计以这种方式工作的用户控件?

【问题讨论】:

    标签: c# asp.net user-controls


    【解决方案1】:

    您可以创建一个自定义控件,这样的东西应该可以工作(让“parsechildren”设置使其在设计器中工作总是很烦人,但我认为这是正确的):

    [ParseChildren(typeof(WebControl), ChildrenAsProperties = true, 
        DefaultProperty = "ChildControls"), NonVisualControl]
    public class EnableGroup: Control
    {
            public EnableGroup() {
                 ChildControls = new Collection<WebControl>();
            }
    
            protected override void OnPreRender(EventArgs e)
            {
                foreach (WebControl ctl in Controls) {
                    ctl.Enabled = this.Enabled;
                }
                base.OnPreRender(e);
            }
    
            public Collection<WebControl> ChildControls
            {
                get; 
                protected set;
            } 
    }
    

    但老实说,简单地编写扩展方法或其他东西可能同样容易,例如

    public static SetChildEnabled(this WebControl parent) {
        foreach (WebControl ctl in parent.Controls) {
             ctl.Enabled = parent.Enabled;
        }
    }
    

    然后只需使用任何旧控件来包装您的组...

    <asp:PlaceHolder runat="server" id="Group1">
    </asp:PlaceHolder>
    

    并在预渲染时调用该代码:

    Group1.SetChildEnabled()
    

    如果需要,您可以很容易地以递归方式处理更深的嵌套子级。

    想一想,您可能只需创建一个扩展 PlaceHolder 的控件并跳过上面的困难部分...如果您希望能够控制容器中允许的对象类型,这就是您的方式这样做,但可能有点矫枉过正。

    【讨论】:

    • 这是一个很好的答案 - 谢谢 - 我现在的想法是我可以迭代 Controls 集合并递归地反映一个名为 Enabled 的布尔属性
    • 您是否知道如何让您的代码支持 UserControls 子控件的场景? (谈论第一种方式 - 制作我自己的服务器控件) - 我得到所有项目类型必须是 WebControl 的错误......只是好奇 - 我现在将尝试从占位符派生的方式......
    • 好的,我明白了 - 将 Web 控件更改为模板控件并使用“is”来确定它是否是我可以启用/禁用的类型。谢谢!我会将此标记为已回答。
    • 关于您的第二条评论 - 我错误地认为 UserControl 是 WebControl 的子类,显然它不是。 UserControl 和 WebControl 的唯一共同祖先似乎是 Control。因此,如果您只需要用户控件,您的解决方案似乎很好,但如果您希望能够放置任何东西,那么您可以使用 Control。
    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    相关资源
    最近更新 更多