Reputation: 107
In my cms I have dynamically created rows and in each row I have one textbox and one checkbox.
'<input value="FALSE" name="DynamicCheckbox" class="switch-input" type="checkbox" /> '
+ '<input class="playerNumber" name="DynamicTextBoxName" type="text" size="2" value="' + valueNumber + '" /> '
From code behind I want to get value from each textbox and checkbox and put them into the List object. Value from textboxes I get without problems but I can not get values from chechboxes. In code behind I have:
string[] textboxNames = Request.Form.GetValues("DynamicTextBoxName");
string[] checkbox= Request.Form.GetValues("DynamicCheckbox");
List<Classes.Player> PlayerList = new List<Classes.Player>();
for (int i = 0; i < textboxNames.Length; i++)
{
Classes.Player p = new Classes.Player();
p.name = textboxNames[i]; //WORKS FINE
p.start11 = checkbox[i]; //DOES NOT WORK WITH THIS CODE
}
Upvotes: 1
Views: 1179
Reputation: 107
OK I got it. According to this post checkboxes are posting value 'true' if and only if the checkbox is checked. Insted of catching checkbox value I used hidden inputs.
JS
$("#btnAdd").live("click", function() {
var chk = $('input[type="checkbox"]').last();
chk.each(function() {
var v = $(this).attr('checked') == 'checked' ? 1 : 0;
$(this).after('<input type="hidden" name="' + $(this).attr('data-rel') + '" value="' + v + '" />');
});
chk.change(function() {
var v = $(this).is(':checked') ? 1 : 0;
$(this).next('input[type="hidden"]').val(v);
})
});
<label class="switch">
<input data-rel="active" value="false" name="DynamicYesNoStart11" class="switch-input" type="checkbox" /><span class="switch-label" data-on="Yes" data-off="No"></span> <span class="switch-handle"></span>
</label>
At code behind instead
string[] checkbox= Request.Form.GetValues("DynamicCheckbox");
I put
string[] checkboxYesNoStart11 = Request.Form.GetValues("active");
Upvotes: 1