954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem using String.Format inside Request.Form

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?

cmhampton
Junior Poster in Training
79 posts since Feb 2008
Reputation Points: 23
Solved Threads: 10
 

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
SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 43
Solved Threads: 68
 

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

cmhampton
Junior Poster in Training
79 posts since Feb 2008
Reputation Points: 23
Solved Threads: 10
 

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

SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 43
Solved Threads: 68
 
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!

cmhampton
Junior Poster in Training
79 posts since Feb 2008
Reputation Points: 23
Solved Threads: 10
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You