Loop Through Controls on Web User Control

2019-09-06 17:23发布

I'm having problems looping through controls that are on my user control.

I have tried the following code, but cannot get it to find the checkboxes that are on the user control. (You can see some of my previous attempts that I have commented out.)

    'For Each Ctrl As Control In Page.Controls
    'For Each Ctrl As Control In Me.Page.Controls
    'For Each ctrl As Control In Request.Form
     '''Dim frm As Control = Me.FindControl("frmDefault")
     '''For Each Ctrl As Control In frm.Controls

    Dim Check As CheckBox

    For Each Ctrl As Control In Me.Controls
        If TypeOf Ctrl Is CheckBox Then
            Check = Ctrl
            ' Do something here...
        End If
    Next

There are multiple chekcboxes on the user control. The code shown above is on the code behind page for the user control.

(The user control is being used in conjunction with my CMS, Sitecore. I'm not sure if this has any effect on the problem I am experiencing or not.)

Any suggestions?

5条回答
萌系小妹纸
2楼-- · 2019-09-06 17:36

Some of your controls have controls. Your loop will ignore those controls. I have some extension methods I use to get all controls (you can specify type CheckBox so you don't need to do the type check in your calling code)

<Extension()> _
Public Function ChildControls(ByVal parent As Control) As List(Of Control)
    Return ChildControls(Of Control)(parent)
End Function

<Extension()> _
Public Function ChildControls(Of T As Control)(ByVal parent As Control) As List(Of T)
    Dim result As New ArrayList()
    For Each ctrl As Control In parent.Controls
        If TypeOf ctrl Is T Then result.Add(ctrl)
        result.AddRange(ChildControls(Of T)(ctrl))
    Next
    Return result.ToArray().Select(Of T)(Function(arg1) CType(arg1, T)).ToList()
End Function
查看更多
聊天终结者
3楼-- · 2019-09-06 17:39

Sitecore has no effect on walking through the control-collection, this should be possible.

Are you looping through the right Control-Collection? Is Me.Controls your Page-, UserControl- or RepeaterItems-Control collection (or another collection)? If the checkboxes are nested in another control, you need to walk to that control-collection.

Maybe you should add your .ascx code so we can see what your control-collection looks like.

查看更多
劳资没心,怎么记你
4楼-- · 2019-09-06 17:44

I finally figured out what is going on.

I have the checkboxes inside different tables. These tables contains runat="server". This table is inside a Div tag that also contains a runat="server".

My code could never find the checkboxes because of this. I had to add a For Each that loops through the Div tag and find the appropriate table(s). I then had to loop through the tables in order to find the checkboxes.

查看更多
不美不萌又怎样
5楼-- · 2019-09-06 17:45

Does this bring up the names of the checkboxes?

For Each Ctrl As Control In Me.Controls
    If TypeOf Ctrl Is CheckBox Then
          MsgBox(Ctrl.Name)
    End If
Next

That should let you know if you are hitting you checkboxes. If not rexamine your page design.

I believe you should have no problem assigning ctrl to check, it should act as a pointer to ctrl. If you have more than one checkbox on the page, do an if statement against the ctrl.name to get the correct one.

查看更多
孤傲高冷的网名
6楼-- · 2019-09-06 17:59

Well, I solved it as follows. (c#)

 foreach (Control c in Page.Controls)
        {
            foreach (Control childc in c.Controls)
            {
                if (childc.ClientID == "menupadraolateral1")
                {
                    foreach (Control cMnuLat in childc.Controls)
                    {

                        //here you can access the controls of usercontrol                           

                    }


                }
            }
        }

Where "menupadraolateral1" is the ID used where the usercontrol is called

I hope I have helped

查看更多
登录 后发表回答