一、存储过程,数据库基于前几篇博文。
二、代码
(1)HTMLPage.htm
<!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>
<title>用户列表</title>
<script type="text/javascript" src="JQueryUi/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="JQueryUi/js/jquery-ui-1.8.16.custom.min.js"></script>
<link type="text/css" rel="Stylesheet" href="JQueryUi/css/ui-lightness/jquery-ui-1.8.16.custom.css" />
<script type="text/javascript">
$(document).ready(function () {
$("#Btn_Add").click(function () {
$("#hidediv").dialog({
modal: true,
title: "增加会员",
resizable: false,
bgiframe: true,
open: function () {
$("#hidediv").html("<div> 用户名:<input id=\"Text1\" type=\"text\" name=\"username\" /> <br /> 密码:<input id=\"Password1\" type=\"password\" name=\"password\" /> <br /> 性别: <select id=\"Select1\" name=\"sex\"> <option value=\"-1\">请选择</option> <option value=\"0\">男</option> <option value=\"1\">女</option> </select> <br />");
},
buttons: { "提交": function () {
if ($("#Text1").val() == "") {
alert("请输入用户名");
return false;
}
if ($("#Password1").val() == "") {
alert("请输入密码");
return false;
}
if ($("#Select1").val() == "-1") {
alert("请选择性别");
return false;
}
$.ajax({
type: "post",
chche: false,
url: "Insert.ashx",
data: "username=" + $("#Text1").val() + "&password=" + $("#Password1").val() + "&sex=" + $("#Select1").val(),
success: function (dt) {
alert(dt);
$("#Submit1").attr("value", "提交").removeAttr("disabled");
$("#Text1").val("");
$("#Password1").val("");
$("#Select1").val("-1");
},
beforeSend: function (dt) {
$("#Submit1").attr({ "value": "正在提交", "disabled": "disabled" });
}
});
return false;
}
},
close: function () {
PageList(1);
}
});
})
PageList(1);
});
function PageList(pageno) {
$.ajax({
type: "get",
cache: false,
url: "GetList.ashx?pageno=" + pageno,
dataType: "json",
success: function (dt) {
var cc = eval(dt);
if (cc.RecordCount > 0) {
var htm = "<table><tr><th>编号</th><th>用户名</th><th>密码</th><th>性别</th><th>操作</th>";
for (var i = 0; i < cc.Data.length; i++) {
htm += "<tr id=\"" + cc.Data[i].Id + "\"><td>" + cc.Data[i].Id + "</td><td>" + cc.Data[i].UserName + "</td><td>" + cc.Data[i].PassWord + "</td><td>" + cc.Data[i].Sex + "</td><td> <span οnclick=\"Edit(" + cc.Data[i].Id + ")\">修改</span> <span οnclick=\"Del(" + cc.Data[i].Id + ")\">删除</span> <span οnclick=\"Info(" + cc.Data[i].Id + ")\">查看</span></td></tr>";
}
htm += "<tr><td colspan=\"5\" align=\"center\">";
if (pageno > 1) {
htm += "<span οnclick=\"PageList(" + (1) + ")\">第一页</span>";
htm += "<span οnclick=\"PageList(" + (pageno - 1) + ")\">上一页</span>";
}
if (pageno < cc.PageCount) {
htm += "<span οnclick=\"PageList(" + (pageno + 1) + ")\">下一页</span>";
htm += "<span οnclick=\"PageList(" + (cc.PageCount) + ")\">最后页</span>";
}
htm += "共" + cc.PageCount + "页码共" + cc.RecordCount + "条记录 当前第" + pageno + "页</td></tr>";
$("#content table").html(htm);
}
}
});
}
function Edit(id) {
$("#hidediv").dialog({
modal: true,
title: "修改会员",
resizable: false,
bgiframe: true,
open: function () {
$("#hidediv").html("<div> 用户名:<input id=\"Text1\" type=\"text\" name=\"username\" /> <br /> 密码:<input id=\"Password1\" type=\"password\" name=\"password\" /> <br /> 性别: <select id=\"Select1\" name=\"sex\"> <option value=\"-1\">请选择</option> <option value=\"0\">男</option> <option value=\"1\">女</option> </select> <br />");
$.ajax({
type: "get",
cache: false,
url: "Select" + id + ".ashx",
dataType: "json",
success: function (dt) {
var cc = eval(dt);
$("#Text1").val(cc.UserName);
$("#Password1").val(cc.PassWord);
$("#Select1").val(cc.Sex);
}
});
},
buttons: { "提交": function () {
if ($("#Text1").val() == "") {
alert("请输入用户名");
return false;
}
if ($("#Password1").val() == "") {
alert("请输入密码");
return false;
}
if ($("#Select1").val() == "-1") {
alert("请选择性别");
return false;
}
$.ajax({
type: "post",
cache: false,
url: "Update.ashx",
data: "id=" + id + "&UserName=" + $("#Text1").val() + "&PassWord=" + $("#Password1").val() + "&Sex=" + $("#Select1").val(),
success: function (dt) {
alert(dt);
PageList(1);
}
});
}
}
});
}
function Del(id) {
$.ajax({
type: "post",
cache: false,
url: "Delete.ashx",
data: "id=" + id,
success: function (dt) {
alert(dt);
$("#" + id).remove();
PageList(1);
}
});
return false;
}
function Info(id) {
$("#hidediv").dialog({
modal: true,
title: "查看会员",
resizable: false,
bgiframe: true,
open: function () {
$.ajax({
type: "get",
cache: false,
url: "Select" + (id) + ".ashx",
dataType: "json",
success: function (dt) {
var cc = eval(dt);
$("#hidediv").html("<div>用户名:" + cc.UserName + "<br/>密码:" + cc.PassWord + "<br/>性别:" + (cc.Sex == 0 ? "男" : "女") + "</div>");
}
});
}
});
}
</script>
<style type="text/css">
*
{
margin: 0px;
padding: 0px;
}
table
{
border-collapse: collapse;
}
table tr th, td
{
border-width: 1px;
border-style: solid;
border-color: Blue;
}
span
{
cursor: pointer;
}
</style>
</head>
<body>
<div>
<input id="Btn_Add" type="button" value="增加用户" />
</div>
<div id="content">
<table>
<tr>
<th>
编号
</th>
<th>
用户名
</th>
<th>
密码
</th>
<th>
性别
</th>
<th>
操作
</th>
</tr>
<tr>
<td colspan="5">
共1页 总共0条记录 当前第1页
</td>
</tr>
</table>
</div>
<div id="hidediv">
</div>
</body>
</html>
(2)Insert.ashx
<%@ WebHandler Language="C#" Class="Insert" %>
using System;
using System.Web;
public class Insert : IHttpHandler
{
public void ProcessRequest (HttpContext context) {
string username = context.Request.Form["username"].ToString();
string password = context.Request.Form["password"].ToString();
string sex = context.Request.Form["sex"].ToString();
ThreeLevelBLL.Users user = new ThreeLevelBLL.Users();
int i = user.UserInsert(new ThreeLevelMODEL.Users(0, username, password, (sex == "0" ? true : false)));
if (i > 0)
{
context.Response.Write("添加成功");
}
else
{
context.Response.Write("添加失败");
}
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
(3)Delete.ashx
<%@ WebHandler Language="C#" Class="Delete" %>
using System;
using System.Web;
public class Delete : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
int id = int.Parse(context.Request.Form["id"].ToString());
ThreeLevelBLL.Users user = new ThreeLevelBLL.Users();
int i = user.UserDelete(id);
if (i > 0)
{
context.Response.Write("删除成功");
}
else
{
context.Response.Write("删除失败");
}
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
(4)Update.ashx
<%@ WebHandler Language="C#" Class="Update" %>
using System;
using System.Web;
public class Update : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string str = string.Empty;
context.Response.ContentType = "text/plain";
int id = int.Parse(context.Request.Form["id"].ToString());
ThreeLevelBLL.Users user = new ThreeLevelBLL.Users();
ThreeLevelMODEL.Users model = user.GetUserId(id);
if (model != null)
{
model.UserName = context.Request.Form["UserName"].ToString();
model.PassWord = context.Request.Form["PassWord"].ToString();
model.Sex = (context.Request.Form["Sex"].ToString() == "1" ? false : true);
user.UserUpdate(model);
}
context.Response.Write("修改成功");
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
(5)Select.ashx
<%@ WebHandler Language="C#" Class="Select" %>
using System;
using System.Web;
public class Select : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string str = string.Empty;
context.Response.ContentType = "text/plain";
int id = int.Parse(context.Request.QueryString["id"].ToString());
ThreeLevelBLL.Users user = new ThreeLevelBLL.Users();
ThreeLevelMODEL.Users model = user.GetUserId(id);
if (model != null)
{
str = "{\"UserName\":\"" + model.UserName + "\",\"PassWord\":\"" + model.PassWord + "\",\"Sex\":\"" + (model.Sex == true ? "0" : "1") + "\"}";
}
context.Response.Write(str);
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
(6)GetList.ashx
<%@ WebHandler Language="C#" Class="GetList" %>
using System;
using System.Web;
public class GetList : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
int pageno = int.Parse(context.Request.QueryString["pageno"].ToString());
System.Text.StringBuilder str = new System.Text.StringBuilder();
ThreeLevelBLL.Users bll = new ThreeLevelBLL.Users();
int PageCount = 0;
int RecordCount=0;
System.Collections.Generic.List<ThreeLevelMODEL.Users> us = bll.UserGetList(5, ref PageCount, pageno, ref RecordCount);
if (us.Count > 0)
{
str.Append("{\"PageCount\":\"" + PageCount + "\",\"RecordCount\":\"" + RecordCount + "\",\"Data\":");
str.Append("[");
for (int i = 0; i < us.Count; i++)
{
str.Append("{");
str.Append("\"Id\":\"" + us[i].Id + "\",");
str.Append("\"UserName\":\"" + us[i].UserName + "\",");
str.Append("\"PassWord\":\"" + us[i].PassWord + "\",");
str.Append("\"Sex\":\"" + (us[i].Sex == true ? "男" : "女") + "\"");
str.Append("}");
if (i != us.Count - 1)
{
str.Append(",");
}
}
str.Append("]}");
}
else
{
str.Append("{\"PageCount\":\"" + 0 + "\",\"RecordCount\":\"" + 0 + "\",\"Data\":\"" + 0 + "\"");
}
context.Response.Write(str.ToString());
}
public bool IsReusable {
get {
return false;
}
}
}
(7)UrlWrite.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///UrlWrite 的摘要说明
/// </summary>
public class UrlWrite:IHttpModule
{
public UrlWrite()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public void Dispose()
{
//throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
System.Text.RegularExpressions.MatchCollection match = System.Text.RegularExpressions.Regex.Matches(context.Request.RawUrl, @"Select(\d)+\.ashx");
if (match.Count == 1 && match[0].Success)
{
string newurl = "/ThreeLevelWeb/Select.ashx?id=" + match[0].Value.Split('.')[0].Substring(6, match[0].Value.Split('.')[0].Length - 6);
context.RewritePath(newurl);
}
}
}
三、尚未仔细测试可能会有错误。