简体   繁体   中英

get multiple checkbox checked value in c#

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

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.

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="" />
 <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()
    });
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM