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?
2
  • 1
    Checkbox is a special case. Look at this SO question. Commented May 12, 2016 at 20:15
  • Naming with numbers at the time of creating each chechbox might be one of options. Commented May 13, 2016 at 0:36

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.