標籤:style blog io ar color sp java for on
今天在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); //相應操作 ...... } } }
asp.net關於Repeater控制項中的全選,大量操作