关于asp.net:找到带有ID的复选框

Find checkbox that tharts with id

我正试图循环通过一系列以特定ID开头的复选框。

这段代码似乎很正常,但是什么也没找到:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    Dim childc As Control
    Dim c as Control
    For Each c In Me.Page.Controls
        For Each childc In c.Controls
            If TypeOf childc Is CheckBox Then
                If CType(childc, CheckBox).Checked Then
                    If childc.ID.StartsWith("ctl00_indexBody_ID_ACTIVITE_") Then
                        i = i + 1
                        Alerte(CType(childc, CheckBox).Text)
                        strSQL ="INSERT INTO A_ACTIVITES VALUES("
                        strSQL += Me.ID_PRESTATAIRE.Text +"," + childc.ID +","
                        strSQL +=")"
                        oWebConnection.Execute(strSQL)
                    End If
                End If
            End If
        Next
    Next

在第二行中小插曲说

1
it's impossible to cast an objet of type 'ASP.masterpage_master' to 'System.Web.UI.WebControls.CheckBox

感谢您的帮助


cchildc声明为Control,而不是CheckBox

c的声明不可见,但我认为您犯了相同的错误。


我找到了解决方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Protected Sub GererActivite(ByVal oControls As ControlCollection)
    Dim oControl As Control
    For Each oControl In oControls
        If oControl.HasControls Then
            GererActivite(oControl.Controls) ' récursivité
        ElseIf TypeOf oControl Is CheckBox Then
            If CType(oControl, CheckBox).Checked Then
                If CType(oControl, CheckBox).Text.StartsWith("ctl00_indexBody_ID_ACTIVITE_") Then

                End If

            End If
        End If
    Next

End Sub

此函数通过控件集合运行,并且如果此集合的控件具有控件,则我使用相同的函数通过这些控件运行,直到它找到没有控件的控件,并且如果它是复选框,则可以检查如我所愿。
它像魅力一样工作。

实际上这是我老板的想法,我只是在吹牛,哈哈。

希望对别人有帮助。