2012年(366)
分类: 系统运维
2012-03-15 21:43:47
$("#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 + "
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 + "
外部函数:
function drop(count) { // 初始化新的下拉页码函数 注意一定要放在$(function(){....})外部
var num = parseInt(count);
$("#Drop").children().remove();
for (var i = 1; i <=num; i++) {
$("#Drop").append("");
}
}
后台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 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 源代码下载地址,含有两篇文章全部源码: