迭代“MainContents”中的每个文本框控件?

发布于 2024-10-05 17:26:15 字数 986 浏览 7 评论 0原文

我可以使用下面的代码迭代“MainContents”中的每个文本框控件。 Q1:有更短的路吗? (获取“MainContents”中的所有控件?)

    For Each ctrl As Control In Page.Controls
        For Each subctrl As Control In ctrl.Controls
            For Each subctrlsub As Control In subctrl.Controls
                If TypeOf subctrlsub Is System.Web.UI.WebControls.ContentPlaceHolder Then
                    If subctrlsub.ClientID = "MainContent" Then
                        For Each ct As Control In subctrlsub.Controls
                            If TypeOf ct Is System.Web.UI.WebControls.TextBox Then
                                For r As Short = 1 To 8
                                    For c As Short = 1 To 6
                                        .... (do something) ...
                                    Next
                                Next
                            End If
                        Next
                    End If
                End If
            Next
        Next
    Next

I'm able to iterate each text box control in "MainContents" by using the code below.
Q1: Is there a shorter way? (to get all controls in "MainContents"?)

    For Each ctrl As Control In Page.Controls
        For Each subctrl As Control In ctrl.Controls
            For Each subctrlsub As Control In subctrl.Controls
                If TypeOf subctrlsub Is System.Web.UI.WebControls.ContentPlaceHolder Then
                    If subctrlsub.ClientID = "MainContent" Then
                        For Each ct As Control In subctrlsub.Controls
                            If TypeOf ct Is System.Web.UI.WebControls.TextBox Then
                                For r As Short = 1 To 8
                                    For c As Short = 1 To 6
                                        .... (do something) ...
                                    Next
                                Next
                            End If
                        Next
                    End If
                End If
            Next
        Next
    Next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-10-12 17:26:15

我将其从 C# 转换为 VB,请原谅潜在的错误。这称为递归。

Protected Sub DoSomething(ctrl As Control)
   For Each c As Control In ctrl.Controls
       If TypeOf c Is ContentPlaceHolder Then
          If c.ClientID = "MainContent" Then
                           // Do your stuff
          End If
       End If

       If c.Controls.Count > 0 Then
          DoSomething(c)
       End If
   Next
End Sub

I converted this from C# to VB, forgive the potential incorrectness. This is called Recursion.

Protected Sub DoSomething(ctrl As Control)
   For Each c As Control In ctrl.Controls
       If TypeOf c Is ContentPlaceHolder Then
          If c.ClientID = "MainContent" Then
                           // Do your stuff
          End If
       End If

       If c.Controls.Count > 0 Then
          DoSomething(c)
       End If
   Next
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文