Problem using String.Format inside Request.Form

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 72
Reputation: cmhampton is an unknown quantity at this point 
Solved Threads: 10
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Problem using String.Format inside Request.Form

 
0
  #1
Feb 28th, 2008
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:
  1.  
  2. If Not (Request.Form(String.Format(_
  3. "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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Problem using String.Format inside Request.Form

 
0
  #2
Feb 28th, 2008
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:
  1. Sub DisableItems(ByVal S As Object, ByVal E As RepeaterEventArgs)
  2. Dim rpc As RepeaterItemCollection = rptItems.Items
  3. Dim chkBox As Boolean = False 'keep if needed
  4.  
  5. For Each Item As RepeaterItem In rpc
  6. Dim MyCheckBox As CheckBox = CType(Item.FindControl("chkIsActive"), CheckBox)
  7. If MyCheckBox.Checked Then
  8. chkBox = True
  9. 'disable whatever here, or put it into an array or string,
  10. 'then disable it all at once later.
  11. End If
  12. Next
  13.  
  14. If chkBox Then
  15. 'disable whatever here, or get rid of this if you disabled earlier.
  16. End If
  17. End Sub
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 72
Reputation: cmhampton is an unknown quantity at this point 
Solved Threads: 10
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: Problem using String.Format inside Request.Form

 
0
  #3
Feb 29th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Problem using String.Format inside Request.Form

 
0
  #4
Feb 29th, 2008
I use that function with a "btnDelete_Click" and it works perfectly. but okay, glad to hear you got it
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 72
Reputation: cmhampton is an unknown quantity at this point 
Solved Threads: 10
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: Problem using String.Format inside Request.Form

 
0
  #5
Feb 29th, 2008
Originally Posted by SheSaidImaPregy View Post
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC