UserList.aspx:
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../../js/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function() {
$("#tb a:contains('删除')").click(function() {
return confirm("您懂删除?");
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<a href="UserAdd.aspx">添加</a>
<div>
<table id="tb">
<tr>
<th>编号</th><th>姓名</th><th>操作</th>
</tr>
<%= TbData %>
</table>
</div>
</form>
</body>
UserList.aspx.cs:
public partial class UserList : System.Web.UI.Page
{
public string TbData { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//后台往前台输出一个集合
BLL.UserInfo userInfoService = new UserInfo();
//后台基类给前台子类传递了一个数据
var users = userInfoService.GetModelList("");
StringBuilder sb = new StringBuilder();
foreach (var userInfo in users)
{
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td><a href='EditUser.aspx?id={0}'>修改</a> <a href='DeleteUser.ashx?id={0}'>删除</a></td></tr>",
userInfo.ID,userInfo.UserName);
}
TbData = sb.ToString();
}
}
UserAdd.aspx:
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>用户名:</td><td><input type="text" name="txtName" /></td>
</tr>
<tr>
<td>密码:</td><td><input type="text" name="txtPwd" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="添加"/>
</td>
</tr>
</table>
</div>
</form>
</body>
UserAdd.aspx.cs:
public partial class UserAdd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)//如果点击了页面的按钮回发回来的话。处理表单添加
{
string name = Request["txtName"];
string pwd = Request["txtPwd"];
Model.UserInfo userInfo =new UserInfo();
userInfo.Mail = string.Empty;
userInfo.Phone = string.Empty;
userInfo.UserName = name;
userInfo.Pwd = pwd;
BLL.UserInfo userInfoService =new BLL.UserInfo();
userInfoService.Add(userInfo);
Response.Redirect("UserList.aspx");
}
}
}
EditUser.aspx:
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" name="id" value="<%=this.CurrentUser.ID %>"/>
<table>
<tr>
<td>用户名:</td><td><input type="text" name="txtName" value="<%=this.CurrentUser.UserName %>"/></td>
</tr>
<tr>
<td>密码:</td><td><input type="text" name="txtPwd" value="<%=this.CurrentUser.Pwd %>"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="修改"/>
</td>
</tr>
</table>
</div>
</form>
</body>
Edit.aspx.cs:
public partial class EditUser : System.Web.UI.Page
{
public Model.UserInfo CurrentUser { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//区别:第一次请求来的,还是点击提交按钮回发回来。
//微软已经帮我们判断好了这是第一次请求过来,还是当前页面被点击了按钮之后,表达回发回来。
//if (Request.HttpMethod.ToLower() == "get")
if(!IsPostBack)
{
//拿到当前要修改用户的ID
int id = int.Parse(Request["id"] ?? "0");
if (id > 0)
{
BLL.UserInfo userInfoService = new UserInfo();
CurrentUser = userInfoService.GetModel(id);
}
}
else
{
//做修改。
//从表达获取数据,然后修改到数据中去就行了。
int id = int.Parse(Request["id"] ?? "0");
string name = Request["txtName"];
string pwd = Request["txtPwd"];
//保存到数据里去
BLL.UserInfo userInfoService =new UserInfo();
var user = userInfoService.GetModel(id);
user.UserName = name;
user.Pwd = pwd;
userInfoService.Update(user);
//页面跳转到列表页面
Response.Redirect("UserList.aspx");
}
}
}
Delete.aspx.cs
public class DeleteUser : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int id = int.Parse(context.Request["id"] ?? "0");
if (id > 0)
{
BLL.UserInfo userInfoService =new UserInfo();
userInfoService.Delete(id);
}
context.Response.Redirect("UserList.aspx");
}
public bool IsReusable
{
get
{
return false;
}
}
}
注意:.aspx中:用<% %>包含后台代码:
<% foreach (var user in this.UserList)
{%>
<li><span>2010-08-18</span><a href="Shownews.html" target="_blank"><%= user.UserName %></a></li>
<%} %>
.aspx.cs:
public partial class NewsList : System.Web.UI.Page
{
public string Data { get; set; }
public List<Model.UserInfo> UserList { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//代码后置类给前台页面(子类)传递数据。父类给子类传数据。
//Data = DateTime.Now.ToString();
//后台往前台输出一个集合
BLL.UserInfo userInfoService =new UserInfo();
//后台基类给前台子类传递了一个数据
UserList = userInfoService.GetModelList("");
}
}