一、UserInfoList.html代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>用户列表</title>
<link href="../Css/tableStyle.css" rel="stylesheet" />
<link href="../Css/themes/default/easyui.css" rel="stylesheet" />
<link href="../Css/pageBarStyle.css" rel="stylesheet" />
<link href="../Css/themes/icon.css" rel="stylesheet" />
<script src="../Js/jquery-1.7.1.js"></script>
<script src="../Js/jquery.easyui.min.js"></script>
<script src="../Js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
$(function () {
$("#detailDiv").css("display", "none");
$("#addUserDiv").css("display", "none");
$("#editUserDiv").css("display", "none");
//$("#dlg").dialog('close');
loadUserInfo(1);
$("#addUserInfo").click(function () {
bindAddClick();
});
});
//展示添加用户数据表单
function bindAddClick() {
$("#addUserDiv").css("display", "block");
$('#addUserDiv').dialog({
width: 300,
height: 300,
title: "添加用户信息",
collapsible: true,
maximizable: true,
resizable: true,
modal: true,
buttons: [{
text: 'Ok',
iconCls: 'icon-ok',
handler: function () {
//获取用户在添加表单中输入的数据,然后同AJAX发送服务端。
addUser();
}
}, {
text: 'Cancel',
handler: function () {
$('#addUserDiv').dialog('close');
}
}]
});
}
//完成用户添加
function addUser() {
//获取用户在添加表单中输入的数据
var pars = $("#addUserForm").serializeArray();//{"txtUserName":"zhangsan",}
$.post("AddUserInfo.ashx", pars, function (data) {
if (data == "ok") {
//清除添加表单中输入的数据
$("#addUserForm input").val("");
//关闭添加窗口
$('#addUserDiv').dialog('close');
//删除表格旧数据
$("#tabList tr:gt(0)").remove();
//重新给表格添加数据
loadUserInfo();
} else {
$.messager.alert("提示","添加失败","error");
}
});
}
//加载用户数据
function loadUserInfo(pageIndex) {
$.post("UserList.ashx", {"pageIndex":pageIndex}, function (data) {
var serverData = $.parseJSON(data);
var serverDataLength = serverData.UList.length;
for (var i = 0; i < serverDataLength; i++) {
$("<tr><td>" + serverData.UList[i].Id + "</td><td>" + serverData.UList[i].UserName + "</td><td>" + serverData.UList[i].UserPass + "</td><td>" + serverData.UList[i].Email + "</td><td>" + ChangeDateFormat(serverData.UList[i].RegTime) + "</td><td><a href='javascript:void(0)' class='deletes' dId='" + serverData.UList[i].Id + "'>删除</a></td><td><a href='javascript:void(0)' class='details' nId='" + serverData.UList[i].Id + "'>详细</a></td><td><a href='javascript:void(0)' class='edits' eId='" + serverData.UList[i].Id + "'>编辑</a></td></tr>").appendTo("#tabList");
}
//将页码条加到DIV上。
$("#pageBarDiv").html(serverData.MyPageBar);
bindMyPageBarClick();//给数字页码加单击事件
bindDetailClick();//注意:一定要表格中所有的数据加载完成了,才能给表格中的超链接加单击事件。
bindDeleteClick();//删除
bindEditClick();//编辑
});
}
//给数字页码加单击事件
function bindMyPageBarClick() {
$(".myPageBar").click(function () {
var pageIndex = $(this).attr('href').split('=')[1];
$("#tabList tr:gt(0)").remove();
loadUserInfo(pageIndex);
return false;//不用在执行超链接。给a标签添加单击事件后会先执行单击事件,再执行超链接事件。
});
}
//展示要修改的数据.
function bindEditClick() {
$(".edits").click(function () {
var id = $(this).attr("eId");
$.post("ShowDetail.ashx", { "id": id }, function (data) {
var serverData = $.parseJSON(data);
$("#editUserId").val(serverData.Id);
$("#eidUserName").val(serverData.UserName);
$("#editUserPwd").val(serverData.UserPass);
$("#editUserMail").val(serverData.Email);
$("#editRegTime").val(ChangeDateFormat(serverData.RegTime));
$("#editUserDiv").css("display", "block");
$('#editUserDiv').dialog({
width: 300,
height: 300,
title: "修改用户信息",
collapsible: true,
maximizable: true,
resizable: true,
modal: true,
buttons: [{
text: 'Ok',
iconCls: 'icon-ok',
handler: function () {
editUserInfo();
}
}, {
text: 'Cancel',
handler: function () {
$('#editUserDiv').dialog('close');
}
}]
});
});
});
}
//完成数据更新
function editUserInfo() {
var pars = $("#editUserForm").serializeArray();
$.post("EditUserInfo.ashx", pars, function (data) {
if (data == "yes") {
//关闭添加窗口
$('#editUserDiv').dialog('close');
//删除表格旧数据
$("#tabList tr:gt(0)").remove();
//重新给表格添加数据
loadUserInfo();
} else {
$.messager.alert("提示", "修改失败", "error");
}
});
}
//展示详细信息
function bindDetailClick() {
$(".details").click(function () {
var id = $(this).attr("nId");
$.post("ShowDetail.ashx", { "id": id }, function (data) {
var serverData = $.parseJSON(data);
$("#userName").text(serverData.UserName);
$("#userPwd").text(serverData.UserPass);
$("#userMail").text(serverData.Email);
$("#detailDiv").css("display", "block");
$('#detailDiv').dialog({
width: 300,
height: 300,
title: "用户详细信息",
collapsible: true,
maximizable: true,
resizable: true,
modal:true,
buttons:[{
text:'Ok',
iconCls:'icon-ok',
handler:function(){
alert('ok');
}
},{
text:'Cancel',
handler:function(){
$('#detailDiv').dialog('close');
}
}]
});
});
});
}
//删除数据
function bindDeleteClick() {
$(".deletes").click(function () {
var id = $(this).attr("dId");
$.messager.confirm("删除提示", "你确定要删除该记录吗?", function (r) {
if (r) {//如果该条件成立,表示用户单击了"确定"按钮
$.post("DeleteUser.ashx", { "id": id }, function (data) {
if (data == "ok") {
//清除表格中原有的数据
$("#tabList tr:gt(0)").remove();
//将新数据加载到表格上。
loadUserInfo();
$.messager.show({
title: '提示',
msg: '删除成功',
showType: 'show'
});
} else {
$.messager.alert("提示","删除失败!!","error");
}
});
}
});
});
}
//将序列化成json格式后日期(毫秒数)转成日期格式
function ChangeDateFormat(cellval) {
var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return date.getFullYear() + "-" + month + "-" + currentDate;
}
</script>
</head>
<body>
<a href="javascript:void(0)" id="addUserInfo">添加用户</a>
<table id="tabList">
<tr><th>编号</th><th>用户名</th><th>密码</th><th>邮箱</th><th>时间</th><th>删除</th><th>详细</th><th>编辑</th></tr>
</table>
<div id="pageBarDiv" class="page_nav"></div>
<!------------详细信息------------------>
<div id="detailDiv">
<table>
<tr>
<td>用户名</td><td><span id="userName"></span></td>
</tr>
<tr>
<td>密码</td><td><span id="userPwd"></span></td>
</tr>
<tr>
<td>邮箱</td><td><span id="userMail"></span></td>
</tr>
</table>
</div>
<!----------------添加用户信息------------------->
<div id="addUserDiv">
<form id="addUserForm">
<table>
<tr><td>用户名</td><td><input type="text" name="txtUserName"/></td></tr>
<tr><td>密码</td><td><input type="password" name="txtUserPwd"/></td></tr>
<tr><td>邮箱</td><td><input type="text" name="txtUserMail"/></td></tr>
</table>
</form>
</div>
<!-------------------编辑用户的数据--------------------->
<div id="editUserDiv">
<form id="editUserForm">
<input type="hidden" name="txtEditUserId" id="editUserId" />
<input type="hidden" name="txtEditRegTime" id="editRegTime" />
<table>
<tr><td>用户名</td><td><input type="text" name="txtEditUserName" id="eidUserName"/></td></tr>
<tr><td>密码</td><td><input type="text" name="txtEditUserPwd" id="editUserPwd"/></td></tr>
<tr><td>邮箱</td><td><input type="text" name="txtEditUserMail" id="editUserMail"/></td></tr>
</table>
</form>
</div>
<!-- <a href="javascript:void(0)" class="easyui-linkbutton" οnclick="$('#dlg').dialog('open')">Open</a>
<div id="dlg" class="easyui-dialog" title="Basic Dialog" data-options="iconCls:'icon-save'" style="width:400px;height:200px;padding:10px">
The dialog content.
</div>-->
</body>
</html>
二、UserList.ashx.cs代码
using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CZBK.ItcastProject.WebApp._2015_6_3
{
/// <summary>
/// UserList 的摘要说明
/// </summary>
public class UserList : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int pageIndex;
if (!int.TryParse(context.Request["pageIndex"], out pageIndex))
{
pageIndex = 1;
}
int pageSize = 5;
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
int pageCount = UserInfoService.GetPageCount(pageSize);//获取总页数.
//判断当前页码值的取值范围。
pageIndex = pageIndex < 1 ? 1 : pageIndex;
pageIndex=pageIndex>pageCount?pageCount:pageIndex;
//获取分页数据
List<UserInfo>list=UserInfoService.GetPageList(pageIndex,pageSize);
//获取页码条。
string pageBar = Common.PageBarHelper.GetPagaBar(pageIndex, pageCount);
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
string str = js.Serialize(new { UList = list, MyPageBar = pageBar });//将数据序列化成JSON字符串。匿名类。
context.Response.Write(str);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
三、ShowDetail.ashx.cs代码
using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CZBK.ItcastProject.WebApp._2015_6_3
{
/// <summary>
/// ShowDetail 的摘要说明
/// </summary>
public class ShowDetail : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int id = Convert.ToInt32(context.Request["id"]);
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
UserInfo userInfo=UserInfoService.GetUserInfo(id);
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
context.Response.Write(js.Serialize(userInfo));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
四、AddUserInfo.ashx.cs代码
using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CZBK.ItcastProject.WebApp._2015_6_3
{
/// <summary>
/// AddUserInfo 的摘要说明
/// </summary>
public class AddUserInfo : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
UserInfo userInfo = new UserInfo();
userInfo.UserName = context.Request["txtUserName"];
userInfo.UserPass = context.Request["txtUserPwd"];
userInfo.Email = context.Request["txtUserMail"];
userInfo.RegTime = DateTime.Now;
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
if (UserInfoService.AddUserInfo(userInfo))
{
context.Response.Write("ok");
}
else
{
context.Response.Write("no");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
五、DeleteUser.ashx.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CZBK.ItcastProject.WebApp._2015_6_3
{
/// <summary>
/// DeleteUser 的摘要说明
/// </summary>
public class DeleteUser : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int id = Convert.ToInt32(context.Request["id"]);
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
if (UserInfoService.DeleteUserInfo(id))
{
context.Response.Write("ok");
}
else
{
context.Response.Write("no");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
六、EditUserInfo.ashx.cs代码
using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CZBK.ItcastProject.WebApp._2015_6_3
{
/// <summary>
/// EditUserInfo 的摘要说明
/// </summary>
public class EditUserInfo : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
UserInfo userInfo = new UserInfo();
userInfo.UserName = context.Request["txtEditUserName"];
userInfo.UserPass = context.Request["txtEditUserPwd"];
userInfo.Email = context.Request["txtEditUserMail"];
userInfo.Id = Convert.ToInt32(context.Request["txtEditUserId"]);
userInfo.RegTime = Convert.ToDateTime(context.Request["txtEditRegTime"]);
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
if (UserInfoService.EditUserInfo(userInfo))
{
context.Response.Write("yes");
}
else
{
context.Response.Write("no");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
七、PageBarHelper.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CZBK.ItcastProject.Common
{
public class PageBarHelper
{
public static string GetPagaBar(int pageIndex, int pageCount)
{
if (pageCount == 1)
{
return string.Empty;
}
int start = pageIndex - 5;//计算起始位置.要求页面上显示10个数字页码.
if (start < 1)
{
start = 1;
}
int end = start + 9;//计算终止位置.
if (end > pageCount)
{
end = pageCount;
//重新计算一下Start值.
start = end - 9 < 1 ? 1 : end - 9;
}
StringBuilder sb = new StringBuilder();
if (pageIndex > 1)
{
sb.AppendFormat("<a href='?pageIndex={0}' class='myPageBar'>上一页</a>", pageIndex - 1);
}
for (int i = start; i <= end; i++)
{
if (i == pageIndex)
{
sb.Append(i);
}
else
{
sb.AppendFormat("<a href='?pageIndex={0}' class='myPageBar'>{0}</a>",i);
}
}
if (pageIndex < pageCount)
{
sb.AppendFormat("<a href='?pageIndex={0}' class='myPageBar'>下一页</a>", pageIndex + 1);
}
return sb.ToString();
}
}
}