一般程序文件代码
using System;
using System.Web;
using System.Linq;
using System.Data.Linq;
public class Shanchu : IHttpHandler {
private MydbDataContext _Context = new MydbDataContext();
public void ProcessRequest (HttpContext context) {
//定义s 等于空
string s = "";
//获取值
string code = context.Request["id"].ToString();
//删除
var query = _Context.Car.Where(p => p.Code == code);
if (query.Count() > 0)
{
//找到第一个
Car data = query.First();
//告诉上下文删除
_Context.Car.DeleteOnSubmit(data);
//返回到数据库
_Context.SubmitChanges();
//如果找到了 把s 设为true
s = "true";
}
else
{
s = "false";
}
//返回xml
context.Response.Write("<?xml version='1.0' ?><ok>" + s + "</ok>");
context.Response.End();
}
HTML界面代码
<title></title>
<style type="text/css">
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
color: #fff;
border-style: solid;
border-color: #666666;
background-color: #ff6a00;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
<script src="jquery-1.8/jquery-1.8.2.min.js"></script>
<script language="javascript">
$(document).ready(function () {
$(".del").click(function () {
//获得要删除的主键值
var key = $(this).attr("aaa");
//每个删除按钮所在的那个tr
var tr = $(this).parents(".teltr");
//ajax发送请求删除汽车数据
$.ajax({
url: "Shanchu.ashx",
data: { id: key },
type: "POST",
dataType: "XML",
success: function (data) {
//解析
var ret = $(data).find("ok").text();
if(ret=="True"||ret=="true")
{
//显示界面
$(tr).remove();
}
}//success
})//ajax
//ajax回调删除当前tr
})//click
})//ready
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table width="100%" class="gridtable">
<tr>
<th>代号</th>
<th>车名</th>
<th>系列</th>
<th>上市时间</th>
<th>油耗</th>
<th>价格</th>
<th></th>
</tr>
</HeaderTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
<ItemTemplate>
<tr class="teltr">
<td><%#Eval("Code") %></td>
<td><%#Eval("Name") %></td>
<td><%#Eval("Brand") %></td>
<td><%#Eval("Time") %></td>
<td><%#Eval("Oil") %></td>
<td><%#Eval("Powers") %></td>
<td><a href="#" aaa="<%#Eval("Code") %>" class="del" ">删除</a></td>
</tr>
</ItemTemplate>
</asp:Repeater>