| | |
Problem using String.Format inside Request.Form
Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved |
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:
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?
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:
vb Syntax (Toggle Plain Text)
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?
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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:
You should check your repeater for its controls, and then check to see which one is a checkbox, follow the example below:
ASP.NET Syntax (Toggle Plain Text)
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 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.
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: Adding HTML controls dynamically in asp.net page
- Next Thread: login validation
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 ajax alltypeofvideos appliances application asp asp.net beginner box browser businesslogiclayer button c# cac checkbox class complex control countryselector dataaccesslayer database datagrid datagridview datalist deployment development dgv dialog dropdownlist dropdownmenu dynamic dynamically edit editing embeddingactivexcontrol feedback fileuploader fill findcontrol flash folder form gridview gudi iis image javascript list listbox login maps mobile mouse mssql nameisnotdeclared news novell numerical opera panelmasterpagebuttoncontrols parent problem project radio redirect registration relationaldatabases reportemail response.redirect richtextbox rows schoolproject search security select sessionvariables silverlight smoobjects software sql sql-server ssl tracking treeview validatedate validation vb.net videos vista visualstudio vs2008 web webapplications webdevelopment webprogramming webservice wizard xsl






