循环用户控件的正确逻辑

发布于 2024-12-05 08:02:02 字数 2453 浏览 3 评论 0原文

我有一个动态网页,可以多次加载用户控件,包括根据需要多次加载用户控件。用户控件内有四个控件:标题标签、Repeater、Placeholder,Repeater 内有一个 AjaxControlToolkit Rating 控件。

该结构可能如下所示:

Webpage
  Placeholder
    UserControl (repeater hidden, no data)
      Placeholder - [UserControl]
        UserControl
          Repeater
            RepeaterItem - [RatingControl]
            RepeaterItem - [RatingControl]
          Placeholder - [UserControl]
            UserControl (placeholder hidden, no data)
              Repeater
                RepeaterItem - [RatingControl]
                RepeaterItem - [RatingControl]
    UserControl
      Repeater
        RepeaterItem - [RatingControl]
        RepeaterItem - [RatingControl]
      Placeholder - [UserControl]
        UserControl (placeholder hidden, no data)          
          Repeater
            RepeaterItem - [RatingControl]

这是我的递归方法:

Protected Sub Get_Ratings(ByVal ctl As Control, ByVal grouptotal As Integer)
    If TypeOf ctl Is PerformanceEvaluationSubcontractorControl Then
        Dim pesctl As Control
        For Each pesctl In ctl.Controls
            If TypeOf pesctl Is PerformanceEvaluationSubcontractorControl Then
                Me.Get_Ratings(pesctl, grouptotal)
            ElseIf pesctl.Controls.Count > 0 Then
                Dim spesctl As Control
                For Each spesctl In pesctl.Controls
                    If TypeOf spesctl Is Repeater Then
                        Dim rptctl As Control
                        For Each rptctl In spesctl.Controls
                            Me.Get_Ratings(pesctl, grouptotal)
                        Next
                    End If
                    If TypeOf spesctl Is PlaceHolder Then
                        Dim plhctl As Control
                        For Each plhctl In spesctl.Controls
                            Me.Get_Ratings(plhctl, grouptotal)
                        Next
                    End If
                Next
            ElseIf TypeOf pesctl Is AjaxControlToolkit.Rating Then
                Dim ajrating As AjaxControlToolkit.Rating = pesctl
                grouptotal = grouptotal + ajrating.CurrentRating
            End If
        Next
    ElseIf ctl.Controls.Count > 0 Then
        Dim sctl As Control
        For Each sctl In ctl.Controls
            Me.Get_Ratings(sctl, grouptotal)
        Next
    End If
End Sub

我的问题是,如何有效地循环这种类型的结构以找到评级控件?

I have a dynamic webpage that loads a user control multiple times, including loading the user control within itself as many times as needed. Within the user control there are four controls: Title Label, Repeater, Placeholder and within Repeater a AjaxControlToolkit Rating control.

The structure can look like the following:

Webpage
  Placeholder
    UserControl (repeater hidden, no data)
      Placeholder - [UserControl]
        UserControl
          Repeater
            RepeaterItem - [RatingControl]
            RepeaterItem - [RatingControl]
          Placeholder - [UserControl]
            UserControl (placeholder hidden, no data)
              Repeater
                RepeaterItem - [RatingControl]
                RepeaterItem - [RatingControl]
    UserControl
      Repeater
        RepeaterItem - [RatingControl]
        RepeaterItem - [RatingControl]
      Placeholder - [UserControl]
        UserControl (placeholder hidden, no data)          
          Repeater
            RepeaterItem - [RatingControl]

Here is my recursive method:

Protected Sub Get_Ratings(ByVal ctl As Control, ByVal grouptotal As Integer)
    If TypeOf ctl Is PerformanceEvaluationSubcontractorControl Then
        Dim pesctl As Control
        For Each pesctl In ctl.Controls
            If TypeOf pesctl Is PerformanceEvaluationSubcontractorControl Then
                Me.Get_Ratings(pesctl, grouptotal)
            ElseIf pesctl.Controls.Count > 0 Then
                Dim spesctl As Control
                For Each spesctl In pesctl.Controls
                    If TypeOf spesctl Is Repeater Then
                        Dim rptctl As Control
                        For Each rptctl In spesctl.Controls
                            Me.Get_Ratings(pesctl, grouptotal)
                        Next
                    End If
                    If TypeOf spesctl Is PlaceHolder Then
                        Dim plhctl As Control
                        For Each plhctl In spesctl.Controls
                            Me.Get_Ratings(plhctl, grouptotal)
                        Next
                    End If
                Next
            ElseIf TypeOf pesctl Is AjaxControlToolkit.Rating Then
                Dim ajrating As AjaxControlToolkit.Rating = pesctl
                grouptotal = grouptotal + ajrating.CurrentRating
            End If
        Next
    ElseIf ctl.Controls.Count > 0 Then
        Dim sctl As Control
        For Each sctl In ctl.Controls
            Me.Get_Ratings(sctl, grouptotal)
        Next
    End If
End Sub

My question is, how do I efficiently loop through this type of structure to find the rating controls?

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

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

发布评论

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

评论(2

魂ガ小子 2024-12-12 08:02:02

抱歉,我的 VB.NET 不是那么好,但是递归函数不会类似于:

Protected Function GetRatings(ByVal control As Control) As IEnumerable(Of AjaxControlToolkit.Rating)
  If (control.Controls.Count > 0) Then
    Dim result As New List(Of AjaxControlToolkit.Rating)()

    For Each child In Control.Controls
      Dim rating As AjaxControlToolkit.Rating = child As AjaxControlToolkit.Rating
      If Not (rating Is Nothing) Then
        result.Add(rating)
      Else
        result.AddRange(GetRatings(child))
      End If  
    Next

    Return result
  Else
    Return Enumerable.Empty(Of AjaxControlToolkit.Rating)()
  End If
End Function

你不能做一个求和吗:

Dim controls As IEnumerable(Of AjaxControlToolkit.Rating) = GetRatings(control)
Dim total As Int32 = Enumerable.Sum(Of AjaxControlToolkit.Rating)(_
    controls, Function(rating) rating.CurrentRating)

如果语法有问题,请告诉我,我通常不会这样做网络

Sorry, my VB.NET isn't all that great, but wouldn't a recursive function look similar to:

Protected Function GetRatings(ByVal control As Control) As IEnumerable(Of AjaxControlToolkit.Rating)
  If (control.Controls.Count > 0) Then
    Dim result As New List(Of AjaxControlToolkit.Rating)()

    For Each child In Control.Controls
      Dim rating As AjaxControlToolkit.Rating = child As AjaxControlToolkit.Rating
      If Not (rating Is Nothing) Then
        result.Add(rating)
      Else
        result.AddRange(GetRatings(child))
      End If  
    Next

    Return result
  Else
    Return Enumerable.Empty(Of AjaxControlToolkit.Rating)()
  End If
End Function

Couldn't you then do a summation:

Dim controls As IEnumerable(Of AjaxControlToolkit.Rating) = GetRatings(control)
Dim total As Int32 = Enumerable.Sum(Of AjaxControlToolkit.Rating)(_
    controls, Function(rating) rating.CurrentRating)

If the syntax is way off, let me know, I don't normally do VB.NET

南笙 2024-12-12 08:02:02

下面的代码非常适合我的需求。我没有测试 AjaxControlToolkit.Rating 类型。

调用子程序

 getcontrols(Page.Controls)

使用Here is the sub

 Sub getcontrols(ByVal obControls As ControlCollection)
    Dim sContent As String = ""

    For Each childcontrol As Control In obControls

        If childcontrol.HasControls Then
            setcontent(childcontrol.Controls)
        End If

        If childcontrol.GetType() Is GetType(AjaxControlToolkit.Rating) Then

            Dim sID = CType(childcontrol, AjaxControlToolkit.Rating).ID
            'DO SOMETHING

        End If
    Next 
End Sub

The code below works perfectly for my needs. I did not test the AjaxControlToolkit.Rating type.

Call the sub with

 getcontrols(Page.Controls)

Here is the sub

 Sub getcontrols(ByVal obControls As ControlCollection)
    Dim sContent As String = ""

    For Each childcontrol As Control In obControls

        If childcontrol.HasControls Then
            setcontent(childcontrol.Controls)
        End If

        If childcontrol.GetType() Is GetType(AjaxControlToolkit.Rating) Then

            Dim sID = CType(childcontrol, AjaxControlToolkit.Rating).ID
            'DO SOMETHING

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