Proper logic for looping through user controls
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)
开发者_StackOverflow中文版 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?
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
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
精彩评论