asp.net中Repeater结合js实现checkbox的全选/全不选
前台界面代码:
<input name="CheckAll" type="checkbox" id="CheckAll" value="" onclick="javascript:FormSelectAll('form1','cboxi','CheckAll',this);" /> //用于选择是否全选的复选框,用javascript函数“FormSelectAll('form1','CheckBox',this); ”实现全选效果,具体参数下文在详细解释 <form id="form1" runat="server"> <asp:Repeater ID="RptList" runat="server" > <ItemTemplate> <input name="cboxi" id="cboxi" runat="server" type="checkbox" value='<%# DataBinder.eval_r(Container.DataItem, "F_JobID")%>' /> //使用Repeater 控件绑定checkbox控件,用于选择单项内容 </ItemTemplate> </asp:Repeater> </form>
下面是js代码:
//全选 //formId:目标复选框所在表单 //EleName:目标的复选框name //CbAllEleName:全选/不全选 的复选框id function FormSelectAll(formId, EleName,CbAllEleId, e) { var Elements = document.getElementById(formId).elements; //获取目标复选框的表单 for (var i = 0; i < Elements.length; i++) { if (Elements[i].type == "checkbox" && Elements[i].name.indexOf(EleName) >= 0) { if (document.getElementById(CbAllEleId).checked) { Elements[i].checked = e.checked;//全选 } else { Elements[i].checked = false;//全不选 } } } }
下面是C#代码:
for (int i = 0; i < this.RptList.Items.Count; i++) { HtmlInputCheckBox CB = (HtmlInputCheckBox)this.RptList.Items[i].FindControl("cboxi"); if (CB.Checked == true) { //如果此复选框已经选中,那就...... } }