【问题标题】:Enumerate all controls in the form枚举表单中的所有控件
【发布时间】:2011-05-18 07:30:44
【问题描述】:
private void EnableControls(bool enable)
        {
            foreach (TextBox t in Page.Form.Controls.OfType<TextBox>())
            {
                t.ReadOnly = !enable;
            }
            chkSameAsCurrent.Enabled = enable;
        }

上面的代码在没有任何母版页的简单页面中运行良好,但如果我在 ContentPage 中运行它,我无法枚举 TextBoxes,甚至无法枚举表单中的任何控件。

【问题讨论】:

    标签: .net asp.net


    【解决方案1】:

    试试这个。我认为这应该可行。

     private void RecursiveLoopThroughControls(Control root)
     {
          foreach (Control control in root.Controls)
          {
              RecursiveLoopThroughControls(control);
              //process the control.
          }
     }
    

    使用方法调用

     RecursiveLoopThroughControls(Page)
    

    【讨论】:

      【解决方案2】:

      您绝对应该通过recursion 进行此操作。试试这个article。 如果您想枚举母版页控件 - 请关注 Page.Master

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-25
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多