0

I am dynamically creating checkbox.

Lets say i add another 2 checkbox by clicking add button which I have written. Now I have

 <input id="personalized1"  name="personalized" type="checkbox" class="form-control" />

 <input id="personalized2"  name="personalized" type="checkbox" class="form-control" />

 <input id="personalized3"  name="personalized" type="checkbox" class="form-control" />

when I submit I am trying to get a list of both checked and unchecked by doing Request.Form("personalized"); but I get ["ON","ON"]. I am not able to get unchecked value.

user3342812
  • 343
  • 8
  • 25
  • You can simply add `runat="server"` to each one of them, and then within submit method check `personalized2.Checked`. Does it helps you? – neoselcev Apr 25 '16 at 22:07
  • Since I am creating them dynamically on the client side that will not work since I would need to update the control everytime a checkbox is added – user3342812 Apr 25 '16 at 22:07
  • Ahhh, sorry men, didn't get that from the post. JS solution can be considered? – neoselcev Apr 25 '16 at 22:10
  • are you saying I get collect all the values and make a ajax post call ? – user3342812 Apr 25 '16 at 22:11
  • 1
    Unchecked checkboxes are not included in form submissions as an HTML standard. You will need to do something like use hidden fields http://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked – Paul Abbott Apr 25 '16 at 22:14
  • Not exactly, but you got the point. On our asp web sites I used to have a hidden field, that was constantly updated with id's of the checkboxes by simple several line jQuery function. And then on submit I just got hidden field value. – neoselcev Apr 25 '16 at 22:17
  • How are you making the checkboxes? If from c# why not use the Checkbox type? – JSON Apr 26 '16 at 02:29

2 Answers2

1

Creating a hidden input field could be a solution. However, when I did this I created the input fields dynamically like you with the below syntax.

<input id='Name' type='checkbox' value='Yes' name='22'>

This can work if you can reference the id, the name given above, at the server side. I iterated through the Request.Form data received and checked each key value pair for values of ON, if the value is on then the check box was true. Then I checked for all missing values to determine which where false.

Joshua Duxbury
  • 4,892
  • 4
  • 32
  • 51
1

you must use a "trick", add an input and an hidden field, example

<input type="checkbox" name="testbox1" value="1">
<input type="hidden" name="testbox1" value="0">

then you'll receive

testbox1=1 if the box is checked

testbox1=0 if the box is unchecked

danilonet
  • 1,757
  • 16
  • 33