-1

I'm trying to loop trough a bunch of checkboxes that start with a specific id.

This bit of code seems normal, but it doesn't find anything :

    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

it breacks in the second line saying that

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

Thank's for your help

TAHIR kamal
  • 75
  • 1
  • 7

2 Answers2

1

Declare c and childc as Control, not CheckBox.

The declaration of c is not visible but I think you made the same mistake.

tezzo
  • 10,858
  • 1
  • 25
  • 48
  • answer updated. if it doesn't work please post your declaration of c variable. – tezzo Jun 19 '15 at 09:59
  • Sorry it worked, but now the loop doesn't get to the sixth line, it's like it doesn't find any checkbox, though there are a lot. – TAHIR kamal Jun 19 '15 at 10:13
  • this is another problem; as stated in your error you are using a master page so you can give a look at this: http://stackoverflow.com/questions/3720590/find-a-control-on-a-page-with-a-master-page – tezzo Jun 19 '15 at 10:21
  • it will help yes, because even when using Findcontrol i don't find the checkboxes, but the loop doesn't even get to the part where it searches by id, it doesn't even find any checkbox, whether it is dynamic or not. – TAHIR kamal Jun 19 '15 at 10:43
  • and as for master page control naming, i named it correctly as ctl00_indexBody_{ID} – TAHIR kamal Jun 19 '15 at 10:44
0

I found a solution

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

This function runs trough a control Collection, and if a control of this collection has controls, i run trough these controls with the same function untill it finds a control that hasn't controls and if it is a checkbox, i can check on it as i wish. It worked like a charm.

In fact it's my bosses idea i'm just bragging around, lol.

Hope it will help others.

TAHIR kamal
  • 75
  • 1
  • 7