tags:

views:

179

answers:

4

is there a way to disable all the all the buttons on the page at once.?

if (Directory.Exists(folder))
            { 
              all buttons enabled
            }
            else
            {
                All buttons disabled 
                Label4.Text = "Agent Share folder does not exists";
            }

any suggestions thanks

+5  A: 
foreach (Button button in this.Controls.OfType<Button>())
    button.Enabled = false;

Edit:

You may actually need to do more than this. The Controls collection only fetches the controls that are the immediate children of a particular parent, and it doesn't recursively search through the entire page to find all the buttons. You could use something like the recursive function on this page to recursively find all the buttons and disable every last one of them.

If you add the code from the above linked page, your code would then be:

foreach (Button button in FindControls<Button>(this))
    button.Enabled = false;

These kind of recursive methods will come in very handy in ASP.NET once you use them a couple of times.

David Morton
+1 for the answer. To further clarify, in the code-behind of any page, you can always get any of the ASP components on the page and interact with them via their ID. So, you could have just said each button by name and disabled it individually, but David's answer is much more succinct.
JasCav
ur explanation was really good... i have a doubt what if i want to disable the entire working of that page and not only the buttons... like no controls on that page should work.. so the user has the only option to go back... do i have to give command like above or is there a simple commad for the page..
Then you'd want to simply do something like "foreach (Control ctl in FindControls<Control>(this)) ctl.Enabled = false;" -- Enabled is a control property, not just a TextBox property.
David Morton
thanks a lot man.. appreciate it..
A: 

pseudocode:

for each (Control c in Page.Controls)
    if (typeof(c) == Button)
         c.enabled = false;
egrunin
David Morton's is better than mine.
egrunin
A: 

Something among the lines might help:

protected void DisableButtons(Control root)
{
     foreach (Control ctrl in root.Controls)
     {
          if (ctrl is Button)
          {
              ((WebControl)ctrl).Enabled = false;
          }
          else
          {
              if (ctrl.Controls.Count > 0)
              {
                   DisableButtons(ctrl);
              }
          }
     }
}

which could be called like this:

protected void Page_Load(object sender, EventArgs e) 
{
    DisableButtons(this);
}
Darin Dimitrov
+1  A: 

As other answers have said, ultimately you will need to cycle through the page and find and disable containing elements. One way of mitigating this might be to place all of the necessary buttons in a panel (or multiple panels) and disable the panels in place of the buttons.

Matthew Jones
disabling panels .... nice