1.首先展示列表 ashx 讲究的是个替换 这些就是属于ashx麻烦的地方
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "text/html"; 4 BLL.UserInfoService UserInfoService = new BLL.UserInfoService(); 5 List<UserInfo>list= UserInfoService.GetEntityList(); 6 // List<UserInfo>list=UserInfoService.GetEntityList(); 7 StringBuilder sb = new StringBuilder(); 8 foreach (UserInfo userInfo in list) 9 { 10 sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td><a href=‘ShowDetail.ashx?id={5}‘>详细</a></td><td>删除</td><td><a href=‘Edit.ashx?id={5}‘>修改</a></td></tr>", userInfo.ID, userInfo.UserName, userInfo.UserPass, userInfo.RegTime.ToShortDateString(), userInfo.Email, userInfo.ID); 11 } 12 string filePath = context.Request.MapPath("UserInfoList.html"); 13 string fileContent = File.ReadAllText(filePath); 14 fileContent= fileContent.Replace("$tbody",sb.ToString()); 15 context.Response.Write(fileContent); 16 }
修改
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "text/html"; 4 int id; 5 if (int.TryParse(context.Request.QueryString["id"], out id)) 6 { 7 //1从Bll 里面获取数据 8 BLL.UserInfoService UserInfoService = new BLL.UserInfoService(); 9 10 UserInfo userInfo =UserInfoService.GetModel(id); 11 //2创建文件名和文件路径 12 string filePath = context.Request.MapPath("Edit.html"); 13 string fileContent = File.ReadAllText(filePath); 14 //3 再替换 15 fileContent= fileContent.Replace("$txtName",userInfo.UserName).Replace("$txtPwd",userInfo.UserPass).Replace("$txtEmail",userInfo.Email).Replace("$txtRegTime",userInfo.RegTime.ToString()).Replace("$txtId",userInfo.ID.ToString()); 16 context.Response.Write(fileContent); 17 } 18 }
Edit.html类
1 <form method="post" action="ProcessEdit.ashx"> 2 <input type="hidden" name="txtId" value="$txtId" /> 3 <table> 4 <tr><td>用户名</td><td><input type="text" name="txtName" value="$txtName" /></td></tr> 5 <tr><td>密码</td><td><input type="text" name="txtPwd" value="$txtPwd" /></td></tr> 6 <!-- <tr><td>邮箱</td><td><input type="text" name="txtEmail" value="$txtEmail"/></td></tr> 7 <tr><td>日期时间</td><td><input type="text" name="txtRegTime" value="$txtRegTime"/></td></tr>--> 8 <tr><td colspan="4"><input type="submit" value="修改用户" /></td></tr> 9 </table> 10 </form>
ProcessEdit.ashx类
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; // UserInfo userInfo = new UserInfo(); //userInfo.UserName=context.Request.Form["txtName"]; //userInfo.UserPass=context.Request.Form["txtPwd"]; //userInfo.RegTime = Convert.ToDateTime(context.Request.Form["txtRegTime"]); //userInfo.Email=context.Request.Form["txtEmail"]; int id=Convert.ToInt32(context.Request.Form["txtId"]); BLL.UserInfoService UserInfoService = new BLL.UserInfoService(); UserInfo userInfo=UserInfoService.GetModel(id);//查询一下,然后再修改, userInfo.UserName = context.Request.Form["txtName"]; userInfo.UserPass = context.Request.Form["txtPwd"]; if (UserInfoService.UpdateEntity(userInfo)) { context.Response.Redirect("UserInfoList.ashx"); } else { context.Response.Write("修改失败"); } }
展示
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "text/html"; 4 int id; 5 if (int.TryParse(context.Request.QueryString["id"], out id)) 6 { 7 BLL.UserInfoService UserInfoService = new BLL.UserInfoService(); 8 UserInfo userInfo=UserInfoService.GetModel(id); 9 string filePath = context.Request.MapPath("ShowDetail.html"); 10 string fileContent = File.ReadAllText(filePath); 11 fileContent = fileContent.Replace("$name", userInfo.UserName).Replace("$pwd",userInfo.UserPass); 12 context.Response.Write(fileContent); 13 } 14 }
<table> <tr><td>用户名</td><td>$name</td></tr> <tr><td>密码</td><td>$pwd</td></tr> </table>
时间: 2024-10-04 21:48:05