disable multiple button controls on the page

2020-07-30 03:57发布

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条回答
干净又极端
2楼-- · 2020-07-30 04:10

pseudocode:

for each (Control c in Page.Controls)
    if (typeof(c) == Button)
         c.enabled = false;
查看更多
成全新的幸福
3楼-- · 2020-07-30 04:11

In a Windows Form environment something like this should work for you:

    private void ToggleActivationOfControls(Control ContainingControl, Boolean ControlIsEnabled)
    {
        try
        {
            foreach (Control ctrl in ContainingControl.Controls)
            {
                if (ctrl.GetType() == typeof(Button))
                { 
                    ctrl.Enabled = ControlIsEnabled;
                }
            }
        }
        catch (Exception ex)
        {
            Trace.TraceError("Error occurred during ToggleActivationOfControls");

        }
    }
查看更多
唯我独甜
4楼-- · 2020-07-30 04:19
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.

查看更多
Viruses.
5楼-- · 2020-07-30 04:23

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);
}
查看更多
兄弟一词,经得起流年.
6楼-- · 2020-07-30 04:26

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.

查看更多
登录 后发表回答