这是一个小样本,我想你可以忽略插入部分,因为你说你已经有了它。这使用 jQuery,您也在使用它。
HTML:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function insertData() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/insertData",
data: "{}",
dataType: "json",
success: function (data) {
if (data.d != null)
alert(data.d);
}
});
}
function retrieveData() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/retrieveData",
data: "{}",
dataType: "json",
success: function (data) {
if (data.d != null) {
for (i = 0; i < data.d.length; i++) {
$("#myTable").append("<tr><td><input type='button' onclick='deleteRow(" + data.d[i].ID + ")' value='Delete'></input></td><td>" + data.d[i].Name + "</td><td>" + data.d[i].LastName + "</td><td>" + data.d[i].Age + "</td></tr>");
}
}
}
});
}
function deleteRow(ID) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/deleteRow",
data: "{'row':'" + ID + "'}",
dataType: "json",
success: function (data) {
if (data.d != null) {
alert(data.d);
}
}
});
}
retrieveData();
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table id="myTable">
</table>
</asp:Content>
ASP.NET:
using System;
using System.Web.Services;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
[WebMethod]
public static List<SampleClass> retrieveData()
{
//Retrieve data from the server and return.
List<SampleClass> tmp = new List<SampleClass>();
tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 1", Age = 44, ID = 1 });
tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 2", Age = 23, ID = 2 });
tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 3", Age = 20, ID = 3 });
tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 4", Age = 45, ID = 4 });
tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 5", Age = 21, ID = 5 });
return tmp;
}
[WebMethod]
public static bool deleteRow(int row)
{
if (row > 0)
return true;
return false;
}
[WebMethod]
public static bool insertData()
{
//Do all of the insert in the server blah blah blah
//and return true or false if it was inserted.
return true;
}
}
public class SampleClass
{
public int ID { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
祝你好运!编辑:现在您还可以看到一个删除示例,其中包括将参数传递给 web 方法。祝你好运。