用ajax实现删除
web.config
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
<connectionStrings>
<add name="DB" connectionString="server=.;database=Test;uid=sa;pwd=123;"/>
</connectionStrings>
<system.codedom>
<compilers>
UI层
<asp:Button ID="Button1" runat="server" Text="删除" CommandArgument='<%# Eval("LeaveID") %>' OnCommand="Button1_Command" />
<input type="button" οnclick='deleteLeave(<%# Eval("LeaveID")%>)' value="使用ajax删除" />
<title></title>
<script src="jQuery1.7.2/jquery-1.7.2.js"></script>
<script type="text/javascript">
function deleteLeave(id) {
if (confirm("你确定要删除吗?")) {
$.post("Handler.ashx",{ id: id }, function (data) {
if (data.state == 1) {
alert(data.message);
//刷新页面
location.reload();
} else {
alert(data.message);
}
}, "json")
}
}
</script>
</head>
一般应用程序 hendler
using Model;
using BLL;
using System.Web.Script.Serialization;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//获取编号
int leaveid = Convert.ToInt32(context.Request.Form["id"]);
OperateResult result = new OperateResult();
//执行删除
if (LeaveBLL.Delete(leaveid) > 0)
{
result.state = 1;
result.message = "删除成功";
}
else
{
result.state = -1;
result.message = "删除失败";
}
//将result进行序列化,返回json格式的字符串
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(result);
context.Response.Write(json);
}
Model
public class OperateResult
{
/// <summary>
/// 状态值
/// </summary>
public int state { get; set; }
/// <summary>
/// 提示信息
/// </summary>
public string message { get; set; }
}