Set Enabled to false for multiple web controls

Discussion in 'ASP .Net' started by elaine, May 24, 2007.

  1. elaine

    elaine Guest

    Hi,

    I have a usercontrol which contains multiple server controls like
    textbox, radiobutton..., I have a public field in the usercontrol
    "Enabled". What i want to do is when the usercontrol's "Enabled" is
    set to false, set all its child controls "Enabled" to false. I know i
    can set them one by one. But there should be a easy way to do it
    like :

    for(int i=0;i<myUserControl.Controls.Count;i++)
    {
    control oControl = (control) myUserControl.Controls(i);
    oControl.Enabled = false;
    }

    Anyone can give me a hint?

    Elaine
    elaine, May 24, 2007
    #1
    1. Advertising

  2. elaine

    Olorin Guest

    On May 23, 9:01 pm, elaine <> wrote:
    > But there should be a easy way to do it
    > like :
    >
    > for(int i=0;i<myUserControl.Controls.Count;i++)
    > {
    > control oControl = (control) myUserControl.Controls(i);
    > oControl.Enabled = false;
    >
    > }
    >
    > Anyone can give me a hint?


    Sorry, I'm not clear on your question...
    ...do you mean that the for loop you wrote up there doesn't work ?
    If it does, that seems fairly simple to me.. maybe I'd try to save a
    line with:

    foreach(Control ctrl in this.Controls)
    ctrl.Enabled=false;

    However, I don't think that the base Control class defines an Enabled
    property, and that might be a problem. Then again, a Control might be
    a label, a text literal, a generic html control, a bunch of things
    were it's not quite clear what it means to be 'enabled' or not. Maybe
    when 'enabled' is turned to false on your user control, you just don't
    want to display any of its children controls ?
    If so, at render time, you could check the value of the user control's
    Enabled property:

    if(this.Enabled)
    base.Render(writer);

    HTH,
    ~O
    Olorin, May 24, 2007
    #2
    1. Advertising

  3. You could use control tree recursion to touch all the controls in the way
    you describe.
    Here are the details:
    http://SteveOrr.net/faq/ControlTreeRecursion.aspx

    --
    I hope this helps,
    Steve C. Orr,
    MCSD, MVP, CSM, ASPInsider
    http://SteveOrr.net


    "elaine" <> wrote in message
    news:...
    > Hi,
    >
    > I have a usercontrol which contains multiple server controls like
    > textbox, radiobutton..., I have a public field in the usercontrol
    > "Enabled". What i want to do is when the usercontrol's "Enabled" is
    > set to false, set all its child controls "Enabled" to false. I know i
    > can set them one by one. But there should be a easy way to do it
    > like :
    >
    > for(int i=0;i<myUserControl.Controls.Count;i++)
    > {
    > control oControl = (control) myUserControl.Controls(i);
    > oControl.Enabled = false;
    > }
    >
    > Anyone can give me a hint?
    >
    > Elaine
    >
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider], May 24, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gill Smith
    Replies:
    4
    Views:
    8,873
    Gish Smith
    May 22, 2004
  2. Dave E

    Enabled set to false but still able to use datagrid

    Dave E, Jun 7, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    243
    Michelle Hlaing
    Feb 15, 2005
  3. Bisser Milanov

    Enabled = False disables controls on all pages. Why?

    Bisser Milanov, Jul 13, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    122
    Bisser Milanov
    Jul 13, 2003
  4. Aymer

    web control enabled=false

    Aymer, Sep 3, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    162
  5. Karen Grube
    Replies:
    0
    Views:
    132
    Karen Grube
    Nov 24, 2004
Loading...

Share This Page