读《掌握 Ajax》之基于Table的无刷新删除操作

本文介绍如何使用ASP.NET结合AJAX技术实现Web应用中的数据展示及删除功能,具体步骤包括构建数据库、读取数据并显示在页面上、通过AJAX调用后端处理程序完成数据删除等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

平台:VS.Net2005或VS.Net2003
准备工作:
新建DataShow.aspx和DataProcess.aspx页面
创建access数据库,名为:ajaxDB.mdb,放在文件夹App_Data下
在数据库中创建表T_user【u_id(主键),u_name,u_sex,u_address】
DataShow用于数据呈现;
DataProcess用于处理各种与数据库的交互操作,并返回数据集或操作结果;
删除操作
1.读取数据库数据并显示到DataShow.aspx页面;
注意:
      其中的Show变量必须是public类型的,以确保前台页面能取到它的值!
     为table表的每行设定行号(tr1,tr2......在删除操作中没有用到行号,留以后备用)
DataShow后台代码如下:

ContractedBlock.gifExpandedBlockStart.gifDataShow后台代码
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.OleDb;
None.gif
None.gif
public partial class DataShow : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public string Show;
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        BindTable();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//从数据库中读取数据并显示到当前页面
InBlock.gif
    private void BindTable()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("App_Data/ajaxDB.mdb");
InBlock.gif        OleDbConnection conn 
= new OleDbConnection(connstr);
InBlock.gif        
if (conn.State == ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            conn.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif        conn.Open();
InBlock.gif        OleDbDataAdapter adapter 
= new OleDbDataAdapter("select u_id,u_name,u_sex,u_address from T_user", conn);
InBlock.gif        DataTable tb 
= new DataTable();
InBlock.gif        adapter.Fill(tb);
InBlock.gif        
for (int i = 0; i < tb.Rows.Count;i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Show 
+= "<tr id='tr"+i+"'>";//行id
InBlock.gif
            Show += "<td>" + tb.Rows[i]["u_id"+ "</td>";
InBlock.gif            Show 
+= "<td>" + tb.Rows[i]["u_name"+ "</td>";
InBlock.gif            Show 
+= "<td>" + tb.Rows[i]["u_sex"+ "</td>";
InBlock.gif            Show 
+= "<td>" + tb.Rows[i]["u_address"+ "</td>";
InBlock.gif            Show 
+= "<td><a href='' onclick='javascript:return update(this);'>修改</a></td>";
InBlock.gif            Show 
+= "<td><a href='' onclick='javascript:return del(this);'>删除</a></td>";
InBlock.gif            Show 
+= "<td><a href='' onclick='javascript:return add();'>增加</a></td>";
InBlock.gif            Show 
+= "</tr>";
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


DataShow前台HTML代码如下:

ContractedBlock.gifExpandedBlockStart.gifDataShow前台HTML代码
ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="DataShow.aspx.cs" Inherits="DataShow" %>
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>无标题页</title>
None.gif    
<script src="./js/myxmlhttp.js" type="text/jscript"></script>  
ExpandedBlockStart.gifContractedBlock.gif      
<script language=javascript>dot.gif
InBlock.gif         
//创建XMLHttpRequest对象
InBlock.gif
         var xmlhttp = new MyXmlHttp();
InBlock.gif         
//弹出删除提示 obj:"删除"对象(位于td内的对象)
InBlock.gif
         function del(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif           
if(confirm("您确定删除?"))confimdel(obj);
InBlock.gif           
return false
ExpandedSubBlockEnd.gif         }

InBlock.gif         
//处理删除操作
InBlock.gif
         //先删除数据库中数据,如果执行成功则删除页面上相应的行 
InBlock.gif
         function confimdel(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif           
var tr=obj.parentElement.parentElement;//本行的tr对象
InBlock.gif
           var url="DataProcess.aspx?Flag=delete&uid="+escape(tr.cells[0].innerText);//处理删除操作
InBlock.gif
           var result=xmlhttp.getPage(url);//返回操作结果
InBlock.gif
           if(result=="delete_1")//操作成功
ExpandedSubBlockStart.gifContractedSubBlock.gif
           dot.gif{   
InBlock.gif              
//tr.tbody.table.deleteRow(tr.rowIndex)       
InBlock.gif
              tr.parentElement.parentElement.deleteRow(tr.rowIndex);//删除页面上相应的行
InBlock.gif
              document.all("lab_flag").innerText="删除成功!";
ExpandedSubBlockEnd.gif           }

InBlock.gif           
if(result=="delete_0")//操作失败
ExpandedSubBlockStart.gifContractedSubBlock.gif
           dot.gif{
InBlock.gif             document.all(
"lab_flag").innerText="删除失败!";
ExpandedSubBlockEnd.gif           }

InBlock.gif             
return false;
ExpandedSubBlockEnd.gif         }

ExpandedBlockEnd.gif      
</script>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif        
<table>
None.gif            
<tr><td>用户编号</td><td>用户名称</td><td>用户性别</td><td>用户地址</td><td>修改</td><td>删除</td><td>增加</td></tr>
None.gif            
<%=Show %>
None.gif        
</table>
None.gif        
<label id="lab_flag" style="color:Red;"></label>
None.gif    
</form>
None.gif
</body>
None.gif
</html>
None.gif


2.点击删除触发del(obj)函数,弹出删除提示;
3.选择确定删除后,触发confimdel(obj)函数;
4.通过“删除”这个连接对象得到其所在的行对象tr;
    var tr=obj.parentElement.parentElement
5.通过行对象取到其第一个td内的用户编号;
  tr.cells[0].innerText
6.将用户编号传到DataProcess.aspx 页面;
 DataProcess后台代码如下:

ContractedBlock.gifExpandedBlockStart.gifDataProcess后台代码
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.OleDb;
None.gif
public partial class DataProcess : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public string Result;
InBlock.gif    
private string Flag,uid;
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (Request.QueryString["Flag"!= null) Flag = Request.QueryString["Flag"];
InBlock.gif        
if (Request.QueryString["uid"!= null) uid = Request.QueryString["uid"];
InBlock.gif        
if (Flag == "delete") FunDel();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//处理删除操作
InBlock.gif
    private void FunDel()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("App_Data/ajaxDB.mdb");
InBlock.gif        OleDbConnection conn 
= new OleDbConnection(connstr);
InBlock.gif        
if (conn.State == ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            conn.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif        conn.Open();
InBlock.gif        OleDbCommand cmd 
= new  OleDbCommand("delete from T_user where u_id="+uid, conn);
InBlock.gif        
int count=cmd.ExecuteNonQuery();
InBlock.gif        cmd.Dispose();
InBlock.gif        
if (conn.State == ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            conn.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (count == 0)
InBlock.gif            Result 
= "delete_0";
InBlock.gif        
else
InBlock.gif            Result 
= "delete_1";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


DataProcess前台HTML代码如下:

ContractedBlock.gifExpandedBlockStart.gifDataProcess前台HTML代码
None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataProcess.aspx.cs" Inherits="DataProcess" %><%=Result%>


7.DataProcess后台通过FunDel()方法对数据库进行操作,并将操作结果存储在全局变量Result中;
8.DataShow页面的xmlhttp.getPage(url)方法获取到DataProcess的操作结果,即Result的值;
9.根据Result返回值的不同对当前页面元素进行操作,即javascript操作html元素;
10.删除table表的一行用以下代码:
     tr.parentElement.parentElement.deleteRow(tr.rowIndex),其中tr表示行对象,如果该行的id为tr1则tr=document.all("tr1")
注意:xmlhttp.getPage(url)方法会取到DataProcess.aspx页面上所有你通过查看源文件看到的东西。
所以<%=Result%>和
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataProcess.aspx.cs" Inherits="DataProcess" %> 在同一行,
否则要处理换行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值