views:

868

answers:

4

I have a asp:panel on the root and a lot of controls inside. When I disable the panel, all child are disbled (Panel.Enabled=false).

But I want some of them, e.g. hyperlink, keep enabled even if the container is disabled.

If I cannot do this in a smarter way, I will have to break down my Panel into many pieces to make it works.


EDIT:

Actually what I am doing is a "READ-ONLY" version of a page and a "UPDATE-ABLE" version of a form.

A: 

well if you don't want to break down the panel then you can loop over its child controls (if there are not too many). Pseudo code:

foreach(var c in panel.controls)
{
    if(c is not a hyperlink) c.Enabled=false;
}
TheVillageIdiot
+1  A: 

If you want to disable most of the controls you want something like

DisableAllChildControls(panel); // disabled all controls within panel
//then enable those you want to keep enabled
HyperlinkControl1InPanel.Enabled=true;
HyperlinkControl2InPanel.Enabled=true;
...

void DisableAllChildControls(WebControl p)
{
   foreach (WebControl c in p.Controls)
   {
      c.Enabled=false;
      DisableAllChildControls(c); //recurse
   }
}

or if you just want all hyperlinks enabled

void DisableAllControlsButHyperlinks(WebControl p)
{
   foreach (WebControl c in p.Controls)
   {
      c.Enabled=(c is HyperLink);
      DisableAllControlsButHyperlinks(c); //recurse
   }
}
wefwfwefwe
Of course I've done that before, but that is a mess because ALL my CONTROLS have different STATUS, which is controlled by another module.And I have to program enable/disable all of them, except few button. i.e. Can I have a "c.Enabled = A || B;" where A is abstract from B? addition, A and B could be changed by any event.
Dennis Cheung
you could pass a `params control[] keepEnabled` parameter and then do `foreach (WebControl c in keepEnabled) c.Enabled=true;`
wefwfwefwe
A: 

Very difficult to understand what exactly you want to achieve. The answers above seem perfectly plausible to me. I have the feeling though, that you want to keep enabled just some specific controls that may be some specific buttons and some specific hyperlinks. Is that correct??

Why don't you think of some kind of approach where you declare the controls (with their id for instance) which you DON'T want to disable?? You could do that by declaring a list of strings representing the control id's that shouldn't be disabled in your page/usercontrol like

string[] controlIdsToKeepEnabled = new string[]{"buttonSubmit", "hyperlinkInfo"}

Then in the method where you recursively disable all the controls of your site you check whether the control is not inside the list "controlIdsToKeepEnabled".

A bit complicated, but could work for the special case you have. This functionality could then be packed nicely within a separate custom server control.

Juri
A: 

Tried all the resolution but it not worked then just started with basics and achived it please see below answer which may help you

foreach (Control c in Panel1.Controls) { if (c is WebControl) { WebControl wc = (WebControl)c; if (c.ID.Equals("ControlID")) { wc.Enabled = true; } else { wc.Enabled = false; } }

                }

as Literal controls?(like contentplaceholder can't be accessed as webcontrol) and the cntrols enable property is available for web and html controls only

Kiran