Loop through all the user controls on a page

后端 未结 4 1068
小蘑菇
小蘑菇 2020-12-19 12:34

Want to loop through all the user controls that exist on the page and get their IDs. How do I do it?

4条回答
  •  醉梦人生
    2020-12-19 13:04

    I created an extension method to do this, which works really nicely with LINQ.

    
    Public Function DecendentControls(ParentControl As Control) As Control()
        Dim controls As New List(Of Control)
    
        For Each myControl As Control In ParentControl.Controls
            controls.Add(myControl)
            controls.AddRange(myControl.DecendentControls)
        Next
    
        Return controls.ToArray
    End Function
    

    Then with LINQ you can do something like this to set all the checkboxes on a page to unchecked:

    For Each myControl As CheckBox In pnlMain.DecendentControls.Where(Function(x) TypeOf x Is CheckBox)
        myControl.Checked = False
    Next
    

提交回复
热议问题