1

In my web form I am generating multiple checkboxes dynamically. hence they are not in the control. I am trying to get the value using Request.Form[name] but it is not correct

<input type="checkbox" name="Suppresision" value="Suppresision" />Suppresision

Now I have a add button which dynamically (using Javascript) add more similar checkbox. So within my table element now I have

 <input type="checkbox" name="Suppresision" value="Suppresision" />Suppresision
 <input type="checkbox" name="Suppresision" value="Suppresision" />Suppresision
 <input type="checkbox" name="Suppresision" value="Suppresision" />Suppresision

How do I try to get the values of all three ? I am doing the same for textbox but when I use Request.Form[textbox name] I get a comma separated values of all of them. But for the checkbox I do Request.Form["Suppresision"] I only get one value that too Suppresision instead of checked or not checked.How do I get all three value even if it not checked

2
  • No it is comma separated value. Also If any of the check box is not checked it does not give any value. For eg chbx1 and chbx3 are checked and chbx2 is not checked. I get the result as "Suppresision,Suppresision". Only two values instead of three Commented Mar 4, 2016 at 2:28
  • remove the values from the checkbox and see what you get? i recon it should be something like true,false. Let me know Commented Mar 4, 2016 at 3:25

3 Answers 3

1

If you absolutely need to get a list of all the checkbox controls you have dynamically added you could assemble them into a hidden input when you submit the form.

You need to include a hidden input for each set of checkboxes you add with a name like name="[checkbox name]_allValues"

<input type="checkbox" name="Suppresision" value="Suppresision1" />Suppresision 1
<input type="checkbox" name="Suppresision" value="Suppresision2" />Suppresision 2 
<input type="checkbox" name="Suppresision" value="Suppresision3"/>Suppresision 3
<input type='hidden' value='' name="Suppresision_allVals">

Then add in this jQuery to loop the checkbox groups and you will have access to the full list of values for each checkbox on the server.

$(document.forms[0]).submit(function(event){
      $('input[type=checkbox]').each(function( index ) { //loop all checkboxes
          $itm = $( this ); 
          $allVals = $('input[name=' + $itm.attr('name') + '_allVals]').first(); 
          if ($allVals.length) {                              //see if we have a hidden input
              $allVals.val($allVals.val()
                  + ($allVals.val().length > 0 ? ',' : ' ')   //add delemiter
                  + ($itm.is(':checked') ? $itm.val() : '')); //add value
          }
      });
  });

This way you will have access to the full list in Request.Form["Suppresision_allVals"] with blank values for unchecked boxes similar to what you have for empty textbox controls now.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad it helped, you can upvote it or mark this as the answer
0

You have same name attribute value for the three checkboxes. You should have different to make sure they can be read separately from the request form's collection on the server side. Also, in case of checkboxes, it should be checked attribute. Hopefully this will put you the right direction.

<input type="checkbox" name="Suppresision1" checked="checked" />
 <input type="checkbox" name="Suppresision2" checked="" />
 <input type="checkbox" name="Suppresision3" checked="" />

Comments

0
 <input type="checkbox" class="chkItems" name="Suppresision1" checked="checked" />
 <input type="checkbox" class="chkItems" name="Suppresision2" checked="" />
 <input type="checkbox" class="chkItems" name="Suppresision3" checked="" />

 var chkValue = [];
 $('.chkItems:checked').each(function(i, e) {
 chkValue.push({ 
      chkItem : $(this).val()
    });
 });

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.