1.前台代码aspx代码
<asp:GridView ID="ShowList" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="修改" ItemStyle-Width="40px">
<ItemTemplate>
<asp:CheckBox ID="ck01" runat="server" />
</ItemTemplate>
<ItemStyle Width="40px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="ClassName" HeaderText="班级名称" />
<asp:BoundField DataField="count" HeaderText="人数" />
<asp:BoundField DataField="adddate" HeaderText="添加时间" />
<asp:BoundField DataField="isdel" HeaderText="删除标记" />
</Columns>
</asp:GridView>
<div><asp:Button runat="server" Text="批量删除" ID="Button1" OnClick="btnDelect_Click" OnClientClick="return shiFComfirm('删除');"/></div>
2.JS校验
//删除前的判断
function shiFComfirm(cz) {
var datagrid = document.getElementById('<%=ShowList.ClientID %>');
var trs = datagrid.getElementsByTagName('tr');
var count = 0;
if (trs.length == 1) {
alert("没有数据需要" + cz + "!");
return false;
}
for (var i = 1; i < trs.length; i++) {
if (trs[i].firstChild.firstChild) {
if (trs[i].firstChild.firstChild.checked) {
count++;
}
}
else if (trs[i].children[0].children[0]) {
if (trs[i].children[0].children[0].checked) {
count++;
}
}
}
if (count > 0) {
var reValue = confirm(cz + "后不可恢复,确定要" + cz + "吗?");
if (reValue == true) {
show('myTable', cz + '处理中,请稍候...');
return true;
}
else {
return false;
}
}
else {
alert("请至少选择一行");
return false;
}
}
3.后台的批量删除按钮单击事件
BLL.Classes bll = new BLL.Classes();
CheckBox cb = null;
int id = 0,c = 0;
for (int i = 0; i < ShowList.Rows.Count; i++)
{
if (ShowList.Rows[i].Cells[0].Controls.Count <= 0)
{
continue;
}
cb = ((CheckBox)ShowList.Rows[i].Cells[0].FindControl("ck01"));
id = int.Parse(((Label)(ShowList.Rows[i].Cells[1].FindControl("lbID"))).Text.ToString());
int rt = int.Parse(ShowList.Rows[i].Cells[2].Text.ToString());
//调用业务层队形的Del方法执行删除
if (cb.Checked)
{
bll.SoftDel(id);
c++;
}
}
if (c > 0)
{
PageHelper.ShowScriptMsg("删除成功!");
List<Model.Classes> list = new BLL.Classes().GetList();
ShowList.DataSource = list;
ShowList.DataBind();
}
else
{
PageHelper.ShowScriptMsg("删除失败!");
}