今天在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 runat="server">
<HeaderTemplate>
<table>
<tr>
<th>选项</th>
<th>数据</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox runat="server" Checked="false" />
</td>
<td>
<asp:Label runat="server" Text='<%#Eval("Label_ID")%>'></asp:Label>
</td>
<tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:CheckBox runat="server" Text="全选/反选"/>
<input 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);
//相应操作
......
}
}
}