Linq + Jquery + Ajax 实现异步分页,批量删除,单个删除,全选,反选 ……

此文章是利用Linq + Jquery + Ajax 异步分页的实现 的后续文章,里面涵盖了,利用客户端复选框进行批量删除,单个删除,全选,反选的各种操作,可以立即用于商业开发!

客户端代码:

$("#quan").click(function () { //全部选择
                $(".check_select").attr('checked', true);
            })
            $("#fan").click(function () { //反选择
                $(".check_select").each(function (i) {
                    $(this).attr("checked", !$(this).attr("checked"));
                })
            })

 $(".deleteone").live("click", function () {  // 进行单个删除,用live绑定Click
                var b = confirm("您确定要删除么,谨慎操作!");
                if (b) {
                    var id = $(this).attr("title");
                    $.ajax({
                        type: "Post",
                        url: "WebForm1.aspx",
                        cache: false,
                        data: { deleteid: id, _pageindex: pageindex },  // 参数回传,删除news的Id,当前处于第几页
                        success: function (msg) {
                            var json = eval(msg);  // 返回的json 序列化,返回这个页面剩余的news
                            if (json.d.toString() == "1") {
                                var str = "";
                                for (var i = 0; i < json.items.length - 1; i++) {
                                    str = str + "<tr class='item' ><td height='20' bgcolor='#FFFFFF'><div align='center'><input class ='check_select' type='checkbox' name='checkbox2' title =" + json.items[i].id.toString() + " value='checkbox' /></div></td>";
                                    str = str + "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>" + json.items[i].name + "</span></div></td><td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>2007-11-16 15:00:20 </span></div></td>";
                                    str = str + "<td bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>tiezhu0902@163.com</span></div></td><td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>南京科技股份有限公司…</span></div></td>";
                                    str = str + "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE4'><img src='images/edt.gif' width='16' height='16' />编辑&nbsp; &nbsp;<img src='images/del.gif' width='16' height='16' />";
                                    str = str + "<a title ='" + json.items[i].id.toString() + "' class='deleteone'>删除</a></span></div></td></tr>";
                                }
                                $("#index").text(json.pageindex);  // 初始化新的当前页面处于第几页
                                pageindex = parseInt(json.pageindex); // 初始化新的当前页面处于第几页
                                $("#PageCount").text(json.pagecount); // 初始化新的总页数

                                drop(json.pagecount); // 初始化新的Drop的下拉页码

                                $("#Count").text(json.count);  // 初始化删除某一个新闻后还有多少新闻
                                $(".item").remove();
                                $("#newslist").append(str);
                            }
                            else {
                                alert("删除失败");
                            }
                        },
                        error: function () {
                            alert("服务器加载失败!");
                        }
                    })
                }
            })

            $("#delete").click(function () { // 进行批量删除

                var str = "";
                var count = 0;
                $(".check_select").each(function (i) {
                    if ($(this).attr("checked")) {
                        str += $(this).attr("title") + ",";  // 序列化要删除的Id
                        count += 1;
                    }
                })
                if (count != 0) {
                    var b = confirm("此删除为批量删除,确定要删除么,谨慎操作!");
                    if (b) {
                        $.ajax({
                            type: "Post",
                            url: "WebForm1.aspx",
                            cache: false,
                            data: { deleteC: str, _pageindex: pageindex }, // 参数回传,删除news的Id,当前处于第几页
                            success: function (msg) {
                                alert(msg);
                                var json = eval(msg);
                                if (json.d.toString() == "1") {
                                    var str = "";
                                    for (var i = 0; i < json.items.length - 1; i++) {
                                        str = str + "<tr class='item' ><td height='20' bgcolor='#FFFFFF'><div align='center'><input class ='check_select' type='checkbox' name='checkbox2' title =" + json.items[i].id.toString() + " value='checkbox' /></div></td>";
                                        str = str + "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>" + json.items[i].name + "</span></div></td><td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>2007-11-16 15:00:20 </span></div></td>";
                                        str = str + "<td bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>tiezhu0902@163.com</span></div></td><td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>南京科技股份有限公司…</span></div></td>";
                                        str = str + "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE4'><img src='images/edt.gif' width='16' height='16' />编辑&nbsp; &nbsp;<img src='images/del.gif' width='16' height='16' />";
                                        str = str + "<a title ='" + json.items[i].id.toString() + "' class='deleteone'>删除</a></span></div></td></tr>";
                                    }
                                    $("#index").text(json.pageindex);
                                    $("#PageCount").text(json.pagecount);
                                    pageindex = parseInt(json.pageindex);
                                    drop(json.pagecount);
                                    $("#Count").text(json.count);
                                    $(".item").remove();
                                    $("#newslist").append(str);
                                }
                                else {
                                    alert("删除失败");
                                }
                            },
                            error: function () {
                                alert("服务器加载失败!");
                            }
                        })
                    }
                }
                else {
                    alert("您没有选择");
                }
            })

外部函数:

 function drop(count) { // 初始化新的下拉页码函数 注意一定要放在$(function(){....})外部
            var num = parseInt(count);
            $("#Drop").children().remove();
            for (var i = 1; i <=num; i++) {
                $("#Drop").append("<option>" + i + "</option>");
            }
        }

后台C#代码:

 void Delete()
        {
            //删除单条记录
            if (Request["deleteid"] != null && Request["_pageindex"] != null)
            {
                int id =int.Parse ( Request["deleteid"].ToString());
                LinqToSqlDataContext ds = new LinqToSqlDataContext();
                var q = from a in ds.Admin where a.Id == id select a;
                try
                {
                    ds.Admin.DeleteAllOnSubmit(q);
                    ds.SubmitChanges();
                    string str = DeleteData(int.Parse(Request["_pageindex"].ToString()), ds);
                    Response.Write(str);
                }
                catch
                {
                    string str = "({'d':'0','items':'xx'})";
                    Response.Write(str);
                }
                finally {
                    Response.End();
                }
            }
            //批量删除
            if (Request["deleteC"] != null && Request["_pageindex"] != null)
            {
                List<string> list = new List<string> ();
                string s = Request["deleteC"].ToString();
                string[] strlist  = s.Split(',');
                list = strlist.ToList();
                 LinqToSqlDataContext ds = new LinqToSqlDataContext();
                 try
                 {
                     ds.Admin.DeleteAllOnSubmit(from a in ds.Admin where list.Contains(a.Id.ToString()) select a);
                     ds.SubmitChanges();
                     string str = DeleteData(int.Parse(Request["_pageindex"].ToString()), ds);
                     Response.Write(str);
                 }
                 catch
                 {
                     string str = "({'d':'0','items':'xx'})";
                     Response.Write(str);
                 }
                 finally { Response.End(); }
                 
                
            }
        }

        //序列化Json 输出
        string DeleteData(int pageindex,LinqToSqlDataContext ds)
        {            
            int _pageindex = pageindex ;           
            var q = (from a in ds.Admin select a).Skip((pageindex - 1) * listcount).Take(listcount);
            if (q.ToList().Count == 0)
            {
                q = (from a in ds.Admin select a).Skip((pageindex - 2) * listcount).Take(listcount);
                _pageindex = pageindex - 1;
                if (pageindex == 1)
                { _pageindex = 1; }
            }
            int count = (from a in ds.Admin select a).ToList().Count;
            int pagecount = count / listcount + (count % listcount > 0 ? 1 : 0);
            pagecount = count == 0 ? 1 : pagecount;
            string str = "({'d':'1','count':'" + count + "','pagecount':'" + pagecount + "','pageindex':'" + _pageindex + "','items':[";          
                foreach (var a in q)
                {
                    str += "{'id':'" + a.Id + "'";
                    str += ",'name':'" + a.Name + "'";
                    str += "},";
                }
                str += "]})";
            return str;
        }

OK,以上就是批量删除单个删除的代码,这个文章和 利用Linq + Jquery + Ajax 异步分页的实现  紧密关联,如有不懂请参见利用Linq + Jquery + Ajax 异步分页的实现 ,

文章中的源码只要少加改动CSS就可以立即进行商业开发,如有不足之处,欢迎指正!

C#技术交流群:139769706  源代码下载地址,含有两篇文章全部源码:http://download.csdn.net/detail/w382200929/4143388

原文地址:https://www.cnblogs.com/Leo_wl/p/2399736.html