repeater 结合checkbox批量删除

时间:2023-03-08 17:37:03

项目中用到这个,从网上搜了搜相关内容,代码如下

<script type="text/javascript">
function checkAllThis(obj) {
var aa = document.getElementsByTagName("input");
for (var i = 0; i < aa.length; i++) {
var e = aa[i];
if ((e.type == 'checkbox')) {
var o = e.name.lastIndexOf('ckbIndex');
if (o != -1) {
e.checked = obj.checked;
}
}

}
}
</script>

<form id="form1" runat="server">
<div>
<div style=" margin:0 0 6px 0px; width:805px;">
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="筛选分类" OnClick="Button1_Click" />
</div>

<asp:Repeater runat="server" ID="Repeater1">
<HeaderTemplate>
<table width="805" height="53" border="0" cellpadding="0" cellspacing="1" bgcolor="#E2E2E2">
<tr>
<td width="143" height="30" align="center" bgcolor="#FFFFFF" class="b1">批量选择<input type="checkbox" id="checkAll" onclick="checkAllThis(this);" /></td>
<td width="543" height="30" align="center" bgcolor="#FFFFFF" class="b1">新闻标题</td>
<td width="142" align="center" bgcolor="#FFFFFF" class="b1">日期</td>
<td width="56" align="center" bgcolor="#FFFFFF" class="b1">修改</td>
<td align="center" bgcolor="#FFFFFF" class="b1" style="width: 59px">删除</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td height="26" align="center" bgcolor="#FFFFFF">
<asp:CheckBox ID="ckbIndex" runat="server" /><%--<%#Eval("n_Title")%>--%><asp:HiddenField
ID="hidValue" Value='<%#Eval("n_ID") %>' runat="server" />
</td>
<td height="26" align="center" bgcolor="#FFFFFF"><%#Eval("n_Title")%></td>
<td align="center" bgcolor="#FFFFFF"><%#Eval("n_time")%></td>
<td align="center" bgcolor="#FFFFFF"><a href="News_Edit.aspx?n_ID=<%#Eval("n_ID")%>&n=edit">修改</a></td>
<td align="center" bgcolor="#FFFFFF">

<a href='News_Edit.aspx?n_ID=<%#Eval("n_ID")%>&n=del' onclick="return confirm('确定删除吗')">删除</a>

</td>
</tr>

</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div style="width:805px; padding:5px;">
<asp:Button ID="btnDelete" runat="server" Text="批量删除"
OnClientClick="return confirm('确定删除吗')" onclick="btnDelete_Click" />
</div>
<div style=" margin:10px 0 0 200px; width:805px;">
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>

</div>
</form>

cs代码如下:

protected void btnDelete_Click(object sender, EventArgs e)
{
string getid = "";
for (int i = 0; i < Repeater1.Items.Count; i++)
{
CheckBox cb = Repeater1.Items[i].FindControl("ckbIndex") as CheckBox;
HiddenField hd = Repeater1.Items[i].FindControl("hidValue") as HiddenField;
if (cb.Checked)
{
getid += hd.Value + ",";

}

}
if (getid == "")
{
Response.Write("<script>alert('请选择删除内容');history.go(-1)</script>");
return;

}
getid = getid.Substring(0, getid.Length - 1);
string sql = "delete from News where n_ID in("+getid+")";
Class1.ExecSql(sql);
Class1.AlertShow("删除成功", "News_Manage.aspx");

}