1

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" />&nbsp'
+ '<input class="playerNumber" name="DynamicTextBoxName" type="text" size="2" value="' + valueNumber + '" />&nbsp'

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
}
  • How can I get string array whith all checkbox values?
  • Also, if I want to change it into a bool array instead of string array, how should I do that?
Del boy
  • 97
  • 1
  • 14
  • 1
    Checkbox is a special case. Look at [this](http://stackoverflow.com/questions/11424037/does-input-type-checkbox-only-post-data-if-its-checked) SO question. – Jeroen Heier May 12 '16 at 20:15
  • Naming with numbers at the time of creating each chechbox might be one of options. – Kay Lee May 13 '16 at 00:36

1 Answers1

1

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");
Community
  • 1
  • 1
Del boy
  • 97
  • 1
  • 14