user3342812
user3342812

Reputation: 343

how to get value of unchecked checkbox in C#

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.

Upvotes: 0

Views: 1635

Answers (2)

danilonet
danilonet

Reputation: 1795

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

Upvotes: 1

Joshua Duxbury
Joshua Duxbury

Reputation: 5260

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.

Upvotes: 1

Related Questions