I'm having trouble with finding a checkbox control on a page. I have a repeater that creates a list of items with checkboxes (so I can activate/deactivate them). The checkboxes are named as follows in the html source:

ctl00$ContentPlaceHolder1$ActivateLevelZero$rptItems$ct100$chkIsActive
ctl00$ContentPlaceHolder1$ActivateLevelZero$rptItems$ct101$chkIsActive
ctl00$ContentPlaceHolder1$ActivateLevelZero$rptItems$ct102$chkIsActive
etc...

When I try to access them in the button_click event, I have a counter variable that starts at 100 and increments each time through the loop (I'm looping through the item collection).

So I use the following code:

If Not (Request.Form(String.Format(_
"ctl00$ContentPlaceHolder1$ActivateLevelZero$rptItems$ct{0}$chkIsActive", count.ToString())) Is Nothing) Then

It never finds the controls this way. However, if I replace {0} with 100 or any other valid number so that the actual form element name is specified, it finds it just fine. Only when I try to do it dynamically.

If I write out the value from the String.Format, it is correct.

Anyone have an idea what's going on?

It's not good practice to check them through HTML, not to mention if anything changes, your entire page is broken.

You should check your repeater for its controls, and then check to see which one is a checkbox, follow the example below:

Sub DisableItems(ByVal S As Object, ByVal E As RepeaterEventArgs)
    Dim rpc As RepeaterItemCollection = rptItems.Items
    Dim chkBox As Boolean = False  'keep if needed

    For Each Item As RepeaterItem In rpc
        Dim MyCheckBox As CheckBox = CType(Item.FindControl("chkIsActive"), CheckBox)
        If MyCheckBox.Checked Then
            chkBox = True
            'disable whatever here, or put it into an array or string,
            'then disable it all at once later.
        End If
    Next

    If chkBox Then
        'disable whatever here, or get rid of this if you disabled earlier.
    End If
End Sub

Thank you for the suggestion, but the problem lies in the fact that this takes place in a button_click event, and therefore before the Page_Init(), so the controls aren't available yet.

I did find a workaround though. Instead of using <ASP:CheckBox controls in the repeater, I decided to use HTML checkboxes instead. This way I can control the name more effectively and it works just fine.

This is for an admin tool that only one person will use. I'm not worried about her changing the html before the form gets submitted, so I think this process will work just fine for this app.

Thanks for the feedback though. I appreciate it.

I use that function with a "btnDelete_Click" and it works perfectly. but okay, glad to hear you got it

I'll have to try it. I'd rather do it that way but I couldn't get it to work. Thanks!

This question has already been answered. Start a new discussion instead.