简体   繁体   中英

How to Enable/Disable a group of controls?

My spec calls for a drop down OR two calender boxes along side a radio button group to control which is active, so I made this user control:

<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" />

在此处输入图片说明

I'd like enable/disable child controls using groups:

<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>

How might I go about designing the user control that works this way?

You could create a custom control, something like this ought to work (getting the "parsechildren" settings to make it work in the designer is always annoying but I think this is right):

[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;
        } 
}

But honestly, it might be just as easy to simply write an extension method or something, eg

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

Then just use any old control to wrap your groups...

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

and call that code on prerender:

Group1.SetChildEnabled()

You could make either work recursively pretty easily to handle deeper nested children if desired.

Come to think of it, you could probably just create a control that extends PlaceHolder and skip the hard part above... if you want to be able to control what object types are allowed in your container, that's how you do it, but may be overkill.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM