943,877 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 3101
  • ASP.NET RSS
Feb 28th, 2008
0

Problem using String.Format inside Request.Form

Expand Post »
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:
vb Syntax (Toggle Plain Text)
  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?
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008
Feb 28th, 2008
0

Re: Problem using String.Format inside Request.Form

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:
ASP.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Feb 29th, 2008
0

Re: Problem using String.Format inside Request.Form

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.
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008
Feb 29th, 2008
0

Re: Problem using String.Format inside Request.Form

I use that function with a "btnDelete_Click" and it works perfectly. but okay, glad to hear you got it
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Feb 29th, 2008
0

Re: Problem using String.Format inside Request.Form

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!
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Adding HTML controls dynamically in asp.net page
Next Thread in ASP.NET Forum Timeline: login validation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC