今天在Repeater控件中碰到一个全选的操作,于是上网查了一下,找到一个觉得比较好,便记录下来,
界面代码简化之后(全选操作):
<script type="text/javascript"> function SelectAll(parentChk, ChildId, bigControlID) { var oElements = document.getElementsByTagName("INPUT"); var bIsChecked = parentChk.checked; for (i = 0; i < oElements.length; i++) { if (IsCheckBox(oElements[i]) && IsMatch(oElements[i].id, ChildId, bigControlID)) { oElements[i].checked = bIsChecked; } } }
function IsMatch(id, ChildId, controlID) { var sPattern = '^' + controlID + '_+.*' + ChildId + '$'; var oRegExp = new RegExp(sPattern); if (oRegExp.exec(id)) return true; else return false; } function IsCheckBox(chk) { if (chk.type == 'checkbox') return true; else return false; } </script> <asp:Repeater ID="Repeater_xx" runat="server"> <HeaderTemplate> <table> <tr> <th>选项</th> <th>数据</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:CheckBox ID="CheckBox_ID" runat="server" Checked="false" /> </td> <td> <asp:Label runat="server" ID="Label_ID" Text='<%#Eval("Label_ID")%>'></asp:Label> </td> <tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <asp:CheckBox runat="server" ID="CheckBoxCz" Text="全选/反选" onclick='SelectAll(this,"CheckBox_ID","Repeater_xx")'/>
<input class="anniu" id="input_gs" type="submit" runat="server" onserverclick="Gssubmit_Click" value="操作" />
后台根据选中项获取当前行的数据然后进行操作
public void Gssubmit_Click(object sender, EventArgs e) { for (int i = 0; i < Repeater_xx.Items.Count; i++) { //获取复选框 CheckBox cb = (CheckBox)rpt_Paper.Items[i].FindControl("CheckBoxCz"); //判断是否被选中 if(cb != null && cb.Checked == true) { Label id = (Label)rpt_Paper.Items[i].FindControl("Lable_ID"); //获取该行ID int ID= Convert.ToInt32(Id.Text); //相应操作 ...... } } }
新闻热点
疑难解答