前臺(tái)代碼復(fù)制代碼 代碼如下:
:%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 language="javascript" type="text/javascript" src="ajax/jquery.js">/script>
script language="javascript" type="text/javascript">
//頁(yè)面的初始化
$(document).ready( function(){ loadUserInfo(); });
//加載用戶信息到頁(yè)面
function loadUserInfo()
{
$.ajax(
{
type:"POST",
url:"Default.aspx",
data:{action:'action'},
success:loadUserInfoCallbace
}
);
}
//頁(yè)面初始化回調(diào)函數(shù)
function loadUserInfoCallbace(r)
{
if(r=="")
{
$("#userInfo").html("暫無(wú)數(shù)據(jù)");
}
else
{
$("#userInfo").html(r);
}
}
//全部選中
function CheckAll(obj)
{
$("input[@type=checkbox][@name=checkItem]").attr("checked",$(obj).attr("checked"));
}
//收集所有選中項(xiàng)
function NumberID()
{
var allcheckboxs=$("input[@type=checkbox][@name=checkItem][checked]");
var ids="";
for(i=0;iallcheckboxs.length;i++)
{
var id=$(allcheckboxs[i]).attr("id").split("_")[1];
ids+=id;
ids+=",";
}
return ids;
}
//刪除用戶
function DeleteUser()
{
if(!window.confirm("您真的要?jiǎng)h除選中用戶信息嗎?"))
{
return;
}
var ids=NumberID();
$.ajax({
type:'POST',
url:'Default.aspx',
data:{action:'Delete',userid:ids},
success:deleteUserCall
});
}
//刪除用戶回調(diào)函數(shù)
function deleteUserCall(r)
{
if(r=="ok")
{
alert("刪除成功");
loadUserInfo();
}
else
{
alert("失敗");
}
}
/script>
/head>
body>
form id="form1" runat="server">
div style="text-align:center" id="userInfo">
/div>
/form>
/body>
/html>
后臺(tái)代碼:
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
string Straction = "";
protected void Page_Load(object sender, EventArgs e)
{
Straction = Request["action"];
if(Straction=="action")
{
UserInfo();
}
if (Straction == "Delete")
{
DeleteUser();
}
}
/// summary>
/// 周昕 2009-6-8號(hào)加載用戶詳細(xì)信息
/// /summary>
public void UserInfo()
{
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = ConfigurationManager.ConnectionStrings["BoBoConn"].ToString();
string sql = "select * from loginuser";
SqlCommand mycom = new SqlCommand(sql, mycon);
mycon.Open();
SqlDataReader myda = mycom.ExecuteReader();
StringBuilder str = new StringBuilder();
str.Append("table>tr>td>input id='checkall' name='checkall' type='checkbox' onclick='CheckAll(this)'/>全選/td>td>用戶名/td>td>用戶全名/td>/tr>");
while (myda.Read())
{
str.Append("tr>td>");
str.Append("input id='checkItem_" + myda["ID"].ToString() + "' type='checkbox' name='checkItem' style='text-align='left' onclick=' NumberID()'/>/td>");
str.Append("td>" + myda["UserName"].ToString() + "/td>");
str.Append("td>" + myda["FullName"].ToString() + "/td>/tr>");
}
str.Append("/table>");
str.Append(" div style=' text-align:center'>input type='button' value='刪除' onclick='DeleteUser()' />/div>");
Response.Clear();
Response.ContentType = "application/text";
Response.Write(str);
Response.End();
}
/// summary>
/// 周昕 2009-6-8 刪除選中用戶的詳細(xì)信息
/// /summary>
public void DeleteUser()
{
//獲取用戶ID
string strID = Request["userid"];
string Userid = strID.Substring(0, strID.Length - 1);
//轉(zhuǎn)換成為數(shù)組
string[] stridArray = Userid.Trim().Split(',');
string sql = "delete from loginuser where ID='" + stridArray[0].ToString() + "'";
for (int i = 0; i stridArray.Length; i++)
{
string id = stridArray[i].ToString();
sql += "or ID='" + id + "'";
}
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = ConfigurationManager.ConnectionStrings["BoBoConn"].ToString();
mycon.Open();
SqlCommand mycom = new SqlCommand(sql, mycon);
int n = (int)mycom.ExecuteNonQuery();
mycon.Close();
if (n > 0)
{
Response.Clear();
Response.ContentType = "application/text";
Response.Write("ok");
Response.End();
}
else
{
Response.Clear();
Response.ContentType = "application/text";
Response.Write("no");
Response.End();
}
}
}
效果圖:
![](/upload/20090612193927698.jpg)
您可能感興趣的文章:- PHP+ajax 無(wú)刷新刪除數(shù)據(jù)
- JQuery實(shí)現(xiàn)Repeater無(wú)刷新批量刪除(附后臺(tái)asp.net源碼)
- jquery 漂亮的刪除確認(rèn)和提交無(wú)刷新刪除示例
- Js實(shí)現(xiàn)無(wú)刷新刪除內(nèi)容