Web三层增删查改(CRUD)
主要为了演示Web三层的结构,具体实现方法在学习过程中可作进一步优化,比如可以用SqlHelper或EF代替DataSet实现增删查改,用“男”和“女”代替True和False表示性别等。为使结构清晰,增删查改在UI层分别建了自己的页面和一般处理程序来实现。Web三层 和WinForm三层最大的区别在UI层,所以在此仅展示UI层。
一、 解决方案资源管理器:(三层结构)

二、UI层(为精简文章,省略增删,和查改类似)
(一)ShowUserInfo.aspx

html代码:
cs代码:
(二)Detail.aspx(查)

html代码:
cs代码:
(三)Update.aspx和Update.ashx(改)
Update.aspx用于展示要修改的数据;Update.ashx用于提交数据库修改数据

html代码:
cs代码:(略)
Update.ashx代码:
主要为了演示Web三层的结构,具体实现方法在学习过程中可作进一步优化,比如可以用SqlHelper或EF代替DataSet实现增删查改,用“男”和“女”代替True和False表示性别等。为使结构清晰,增删查改在UI层分别建了自己的页面和一般处理程序来实现。Web三层 和WinForm三层最大的区别在UI层,所以在此仅展示UI层。
一、 解决方案资源管理器:(三层结构)

二、UI层(为精简文章,省略增删,和查改类似)
(一)ShowUserInfo.aspx

html代码:
<form id="form1" runat="server">
<div>
<table>
<thead>
<tr>
<th>姓名</th><th>年龄</th><th>性别</th>
</tr>
</thead>
<tbody>
<%for (int i = 0; i < userInfos.Count; i++)
{ %>
<tr>
<td><%=userInfos[i].UName %></td>
<td><%=userInfos[i].Age %></td>
<td><%=userInfos[i].Gender %></td>
<td>
<a href="Detail.aspx?autoId=<%=userInfos[i].AutoId %>">详情</a>
<a href="Update.aspx?autoId=<%=userInfos[i].AutoId %>">修改</a>
<a href="Delete.aspx?autoId=<%=userInfos[i].AutoId %>">删除</a>
</td>
</tr>
<%} %>
<tr><td colspan="2"><a href="Add.htm">新增一条客户信息</a></td></tr>
</tbody>
</table>
</div>
</form>
cs代码:
public partial class ShowUserInfo : System.Web.UI.Page
{
UserInfoBll userInfoBll = new UserInfoBll();
public List<UserInfoModel> userInfos = new List<UserInfoModel>();
protected void Page_Load(object sender, EventArgs e)
{
userInfos = userInfoBll.LoadAllUserInfos();
}
}
(二)Detail.aspx(查)

html代码:
<form id="form1" runat="server">
<div>
<table>
<tr><td>autoId:</td><td><%=userInfo.AutoId %></td></tr>
<tr><td>姓名:</td><td><%=userInfo.UName %></td></tr>
<tr><td>年龄:</td><td><%=userInfo.Age %></td></tr>
<tr><td>身高:</td><td><%=userInfo.Height %></td></tr>
<tr><td>性别:</td><td><%=userInfo.Gender %></td></tr>
</table>
</div>
</form>
cs代码:
public partial class Detail : System.Web.UI.Page
{
UserInfoBll userInfoBll = new UserInfoBll();
public UserInfoModel userInfo = null;
protected void Page_Load(object sender, EventArgs e)
{
userInfo = userInfoBll.SelectUserInfo(Convert.ToInt32(this.Request["autoId"]));
}
}
(三)Update.aspx和Update.ashx(改)
Update.aspx用于展示要修改的数据;Update.ashx用于提交数据库修改数据

html代码:
<form id="form1" runat="server" method="post" action="Update.ashx">
<div>
<table>
<tr><td>autoId:</td><td><%=userInfo.AutoId %><input type="hidden" name="autoId" value="<%=userInfo.AutoId %>" /></td></tr>
<tr><td>姓名:</td><td><input type="text" name="uName" value="<%=userInfo.UName %>" /></td></tr>
<tr><td>年龄:</td><td><input type="text" name="age" value="<%=userInfo.Age %>" /></td></tr>
<tr><td>身高:</td><td><input type="text" name="height" value="<%=userInfo.Height %>" /></td></tr>
<tr><td>性别:</td><td><input type="text" name="gender" value="<%=userInfo.Gender %>" /></td></tr>
<tr><td colspan="2">确认修改?</td></tr>
<tr><td><input type="submit" value="确定" /></td><td><a href="ShowUserInfo.aspx">返回</a></td></tr>
</table>
</div>
</form>
cs代码:(略)
Update.ashx代码:
public class Update : IHttpHandler {
DemoBLL.UserInfoBll userInfoBll = new DemoBLL.UserInfoBll();
public void ProcessRequest(HttpContext context) {
int autoId = Convert.ToInt32(context.Request["autoId"]);
string uName = context.Request["uName"];
int age = Convert.ToInt32(context.Request["age"]);
int? height = context.Request["height"] == "" ? null : (int?)Convert.ToInt32(context.Request["height"]);
bool? gender = context.Request["gender"] == "" ? null : (bool?)Convert.ToBoolean(context.Request["gender"]);
userInfoBll.UpdateUserInfo(uName, age, height, gender, autoId);
context.Response.Redirect("ShowUserInfo.aspx");
}
public bool IsReusable
{
get {
return false;
}
}
}