【问题标题】:How to loop through all controls in All ASP.NET UpdatePanels in the page?如何遍历页面中所有 ASP.NET UpdatePanels 中的所有控件?
【发布时间】:2014-06-21 01:45:30
【问题描述】:

我正在使用以下代码来循环浏览我的控件。问题是我已将所有这些控件(包括 TextBox 和 DropDownList)放在 AJAX UpdatePanel 控件中,而这些面板永远无法访问。我在页面中有大约三个 UpdatePanel,所以如何循环遍历每个 UpdatePanel 中的所有控件 TextBox 和 DropDownList?

此代码仅适用于一个 UpdatePanel:

foreach (Control c in UpdatePanel2.Controls)
        {
            foreach (Control ctrl in c.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Text = string.Empty;
                }
            }
        }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    它应该可以工作:

    foreach (Control c in Page.Controls)
        {
            if (c is UpdatePanel)
            {
                foreach (Control ctrl in c.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        ((TextBox)ctrl).Text = string.Empty;
                    }
                }
            }
        }
    

    【讨论】:

    • 不得不投票,因为提出问题的白痴没有接受答案。
    【解决方案2】:

    要在UpdatePanel 中滚动控件,请执行以下操作:

    For Each ctrl As Control In UpdatePanel1.ContentTemplateContainer.Controls
        If ctrl.GetType() Is GetType(TextBox) Then
    
        End If
    Next
    

    【讨论】:

      【解决方案3】:

      Itiel 的回答在我的场景中不起作用。我在更新面板中有 6 个网格视图。因为它只是我在 UpdatePanel 中寻找的一种控制类型,所以下面的例程就像一个魅力!我的目标是通过单击调用 ChangeControlStatus(true or false) 方法的按钮来启用或禁用页面上的所有网格视图。也许它会帮助某人。

      private void ChangeControlStatus(bool status)
          {
              int i = 1;
              //loop through 6 gridviews
              for (i = 1; i <= 6; i++)
              {
                  //enable/disable all grids on the page  
                  GridView gv = UpdatePanel1.FindControl("UpdatePanel1").FindControl("Gridview" + i) as GridView;
                  gv.Enabled = status;
              } }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 2021-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多