一般處理程式代碼:
using System;
using System.Web;
using Ajax無重新整理删除.DataSetDeleteDataTableAdapters;
namespace Ajax無重新整理删除
{
/// <summary>
/// DeleteDataById 的摘要說明
/// </summary>
public class DeleteDataById : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int id = Convert.ToInt32(context.Request["id"]);
new T_CommentTableAdapter().DeleteDataById(id);
}
public bool IsReusable
get
{
return false;
}
}
}
Aspx頁面代碼:主要就是JQuery中的代碼;
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Ajax無重新整理删除.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(function () {
//給有isremove=true屬性的控件添加click事件;
$("input[isremove=true]").click(function () {
var id = $(this).attr("rowid"); //擷取目前行的id
//發送請求
$.post("DeleteDataById.ashx", { "id": id }, function (data, status) {
if (status == "success") {
//如果處理成功,remove目前行 parent找他的父級元素
$("input[rowid=" + id + "]").parent().parent().hide("slow");
} else {
alert("删除失敗");
return;
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="Ajax無重新整理删除.DataSetDeleteDataTableAdapters.T_CommentTableAdapter" UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="Original_Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="IpAddress" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
<asp:Parameter Name="PostDate" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
</UpdateParameters>
</asp:ObjectDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="ObjectDataSource1"
InsertItemPosition="LastItem">
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>
未傳回資料。
</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
</td>
&nbsp;
<asp:TextBox ID="IpAddressTextBox" runat="server" Text='<%# Bind("IpAddress") %>' />
<asp:TextBox ID="CommentTextBox" runat="server" Text='<%# Bind("Comment") %>' />
<asp:TextBox ID="PostDateTextBox" runat="server" Text='<%# Bind("PostDate") %>' />
</tr>
</InsertItemTemplate>
<ItemTemplate>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編輯" />
<input id="btnDelete" type="button" isremove="true" rowid='<%#Eval("id") %>' value="無重新整理删除" />
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
<asp:Label ID="IpAddressLabel" runat="server" Text='<%# Eval("IpAddress") %>' />
<asp:Label ID="CommentLabel" runat="server" Text='<%# Eval("Comment") %>' />
<asp:Label ID="PostDateLabel" runat="server" Text='<%# Eval("PostDate") %>' />
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">
</th>
Id
IpAddress
Comment
PostDate
</tr>
<tr id="itemPlaceholder" runat="server">
</table>
<td runat="server" style="">
</LayoutTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>