开发者

disable multiple button controls on the page

开发者 https://www.devze.com 2022-12-21 12:09 出处:网络
is there a way to disable all the all the buttons on the page at once.? if 开发者_如何学Python(Directory.Exists(folder))

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

if 开发者_如何学Python(Directory.Exists(folder))
            { 
              all buttons enabled
            }
            else
            {
                All buttons disabled 
                Label4.Text = "Agent Share folder does not exists";
            }

any suggestions thanks


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.


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.


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");

        }
    }


pseudocode:

for each (Control c in Page.Controls)
    if (typeof(c) == Button)
         c.enabled = false;


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号