转自:http://www.cnblogs.com/Fred_Xu/archive/2013/01/16/how-to-get-the-checkbox-value-from-a-dynamically-generated-checkbox-list-asp-net.html

前端页面需要有一个动态增加表格行的功能,引用了table.addrow.js这个jquery插件,每一行有一个checkbox,name统一为cbMaintainRatio,而鉴于这部分是一个纯Html Form的提交非用户控件,所以我们在后端使用Request.Form来获取值,但问题出来了:

 <table border=" class="atable">
             <tbody><tr class="evenRow">
                 <th>
                     width(px)
                 </th>
                 <th>
                     height(px)
                 </th>
                 <th>maintain ratio</th>
                 <th></th>
             </tr>
             <tr class="cloneRow9674 oddRow">
                 <td>
                     <input type=" name="imgwidth">
                 </td>
                 <td>
                     <input type=" name="imgheight">
                 </td>
                 <td>
                     <input type="checkbox" name="maintainratio">
                 </td>
                 <td class="btnCol">
                     <input type="button" value="Remove" class="delRow delRow9674" style="display: inline-block;">
                 </td>
             </tr><tr class="cloneRow9674 evenRow">
                 <td>
                     <input type=" name="imgwidth">
                 </td>
                 <td>
                     <input type=" name="imgheight">
                 </td>
                 <td>
                     <input type="checkbox" name="maintainratio">
                 </td>
                 <td class="btnCol">
                     <input type="button" value="Remove" class="delRow delRow9674" style="display: inline-block;">
                 </td>
             </tr><tr class="cloneRow9674 oddRow">
                 <td>
                     <input type=" name="imgwidth">
                 </td>
                 <td>
                     <input type=" name="imgheight">
                 </td>
                 <td>
                     <input type="checkbox" name="maintainratio">
                 </td>
                 <td class="btnCol">
                     <input type="button" value="Remove" class="delRow delRow9674" style="display: inline-block;">
                 </td>
             </tr>
             <tr class="evenRow">
             <td colspan="><input type="button" value="Add" class="alternativeRow addRow9674"></td>
             </tr>
         </tbody></table>

如果我们有多行表单,也就是有多个name为cbMaintainRatio的checkbox,post到后端的form,我们通过Request.Form["cbMaintainRatio"]只能获取到一个值"on",而不是像<input type="text" name="width" />这种获取到的"100,200,"值。

浏览了一遍addrow插件的文档,他竟然不支持event,好吧...那只能我们自己来改造代码了:

页面增加一个hidden input,目的为保存多个checkbox的值,如果选中则设定为true,否则false,然后用,分割赋值给这个hidden input

 function setMaintainRatio() {

var fields;
$("input[name='cbMaintainRatio']").each(function () {
var txt = $("input[name='cbMaintainRatioList']");
fields = ($("input[name='cbMaintainRatio']").map(function () {
if (this.checked)
return "1";
else
return "0";
}).get().join(","));

$(txt).val(fields);
});

        }

提交Form的按钮绑定上面这个js 方法:

  <asp:Button ID="btwImageCreate" runat="server" Text="Image Create" OnClick="btwImageCreate_Click" OnClientClick="setMaintainRatio(); return true" />
         <input type="hidden" name="cbMaintainRatioList" /> 

OK,这样我们就可以在后台代码通过Request.Form的形式获取到每一行这个name="cbMaintainRatio" checkbox的值了!

全部和已选择比较

  ; i < Model[].Count; i++)
                     {
                         bool c = false;
                         ; j < Model[].Count; j++)
                         {
                             ][j].ButtonID == Model[][i].ButtonID)
                             {
                                 c = true;
                                 break;
                             }

                         }
                         if (c)
                         {
                             <div class="check-box">

                                 <input name="btns" id="Model[0][i].ButtonID" checked="checked" class="input-text" type="checkbox" style="width: 100px" autocomplete="off" />

                                 <lable>Model[][i].Name</lable>
                             </div>
                         }
                         else
                         {
                             <div class="check-box">

                                 <input name="btns" id="Model[0][i].ButtonID" class="input-text" type="checkbox" style="width: 100px" autocomplete="off" />

                                 <lable>Model[][i].Name</lable>
                             </div>
                         }

                     }

通过Request.Form获取同name的checkbox所有值的更多相关文章

  1. request.getParameterMap() 获取表单提交的键值对 并且 也能获取动态表单的key

    Map<String,String[]> map = request.getParameterMap();Set<String> keys = map.keySet(); 获取 ...

  2. jquery获取复选框checkbox的值

    jQuery API : each(callback) :以每一个匹配的元素作为上下文来执行一个函数. :checked :匹配所有选中的被选中元素(复选框.单选框等,不包括select中的optio ...

  3. Js获取复选框checkbox的值

    var emps= $("input[name='emp']:checked"); var employee= ""; emps.each(function() ...

  4. Request.Form()的使用

    在CS文件中获得对应页面中的下拉框DropDownList_sitebranch值可以有以下几种方法获得:         siteInfo.FZJGID = DropDownList_sitebra ...

  5. ASP中 Request.Form中文乱码的解决方法

    分享下解决方法直接用request.Form()获取的是所有数据所以会有乱码(具体原因不祥) 用 VBScript code Foreach obj in Request.Form Response. ...

  6. [转载]Request、Request.Form和Request.QueryString的区别

    Request.Request.Form和Request.QueryString的区别 request本身是一个系统的静态对象,本身也可以作为数组调用,比如request("abc" ...

  7. Nancy获取Request.Form的所有参数

    Nancy是一个轻量级的webapi框架,在用Nancy框架的时候,我们经常要获取到接口的所有动态参数值, 但是Nancy的Request.Form不能够直接转成NameValueCollection ...

  8. 解决当FORM的ENCTYPE="multipart/form-data" 时request.getParameter()获取不到值的方法

    部分转载于: http://blog.csdn.net/georgejin/article/details/1706647 http://www.cnblogs.com/loveyunk/p/6089 ...

  9. 获取Request.Form所有内容

    string wwww = "";        for (int i = 0; i < Request.Form.Count; i++)        {          ...

随机推荐

  1. jmap之使用说明与JVM配置

    详情可参见:http://blog.csdn.net/fenglibing/article/details/6411953. 1 2. 3.vi 打开查看,具体介绍请看上述链接. 4.查看tomcat ...

  2. python入门一

    python类型 编程语言主要从以下几个角度为进行分类,编译型和解释型.静态语言和动态语言.强类型定义语言和弱类型定义语言. 编译型和解释型 编译型:在程序执行之前,有一个单独的编译过程,将程序翻译成 ...

  3. JSON相关基础知识

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...

  4. Win10 UI入门RelativePanel

    <RelativePanel Background="Black" > <Rectangle x:Name=" RelativePanel.AlignH ...

  5. Asp.net中导出Excel文档(Gridview)

    主要思路,通过GridView来导出文档. 新建一个Aspx页面,页面创建GridView控件,后台绑定好数据源.然后load中直接打印即可导出 前台的GridView <asp:GridVie ...

  6. iOS深入学习(Block全面分析)

    本文翻译自苹果的文档,有删减,也有添加自己的理解部分. 如果有Block语法不懂的,可以参考fuckingblocksyntax,里面对于Block 为了方便对比,下面的代码我假设是写在ViewCon ...

  7. 简单的网络引导安装CentOS7

    实验室有几台电脑,里边装有windows,因为实验需求要给其装入CentOS7.但是这几个电脑无法用U盘引导系统的安装,虽然带有光驱,但是又不想麻烦去买碟片,所以便想到用网络引导系统的安装. 1. 软 ...

  8. 近半年MVC使用后的一些习惯

    半年前接新项目, 来了一个前端, 由于只有我前后台都会, 就做业务层+辅助前端显示, 于是我决定使用MVC 上面那句无关紧要的话让我改了好多遍, 转载请注明出处: http://www.cnblogs ...

  9. 洛谷 P1204 [USACO1.2]挤牛奶Milking Cows Label:模拟Ex 74分待查

    题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民在1500秒开 ...

  10. SSH三大框架的JAR包下载地址

    官网的英文网站读起来有点费劲,把下载地址直接放到这儿,以后免得到处找了 Struts 2 : http://struts.apache.org/download.cgi#struts216 sprin ...