日期:2014-05-16  浏览次数:20729 次

AJAX做表格增删改查问题,路过的朋友目测目测!
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AJAX.aspx.cs" Inherits="AJAX" %>

<!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>
  <style>
  table{text-align:center}
  tr{border:1px solid red}
  td{border:1px solid red}
  </style>
  <script src="json.js" type="text/javascript"></script>
  <script>
  function del(obj, id) {

  var xmlhttp = new XMLHttpRequest();


  xmlhttp.open("post", "del.aspx?id=" + id, true);

  xmlhttp.onreadystatechange = function () {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

  if (xmlhttp.responseText == "ok") {

  document.getElementById("tb").deleteRow(obj.parentNode.parentNode.rowIndex);
  }

  }
  }

  xmlhttp.setRequestHeader("enctype", "application/x-www-form-urlencoded");
  xmlhttp.send(null)
  }
  </script>
</head>
<body>
  
  <table style="border:1px">
  <tr>
  <td colspan=5>增删改查</td>
  </tr>
  <tr>
  <td>编号</td>
  <td>姓名</td> 
  <td>内容</td>
  <td colspan=2>操作</td>
   
  </tr>
  <%foreach (Dictionary<string, object> row in rows)
  {%>
  <tr>
  <td><%=row["a"] %></td>
  <td><%=row["b"] %></td>
  <td><%=row["c"] %></td>
  <td><a href="#" onclick="del(this,'<%=row["a"] %>')">删除</a></td>
  <td><input type=button value="修改" /></td>
  </tr>
  <%} %>
  </table>
</body>
</html>


然后是服务端的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class del : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
   
  if (Request["id"] != null)
  {
  Dictionary<string, object> dic = new Dictionary<string, object>();
  dic.Add("a", Request["id"]);
  if (BUS.BUS.AddOrDel(dic, "tt", false))
  {
  Response.Write("ok");
   
  Response.End();
  }
  else
  {
  Response.Write("no");