在用户列表中链接数据库 在这里Html只管做Post 请求,其他逻辑全部交给ashx 其中修改需要隐藏域
1.展现列表
1 public void ProcessRequest (HttpContext context) { 2 context.Response.ContentType = "text/html"; 3 //连接数据库 4 string connString = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; 5 6 using (SqlConnection conn = new SqlConnection(connString)) 7 { 8 using(SqlDataAdapter apter=new SqlDataAdapter("select * from UserInfo",conn)) 9 { 10 DataTable da = new DataTable(); 11 apter.Fill(da); 12 // 13 StringBuilder sb = new StringBuilder(); 14 for (int i = 0; i < da.Rows.Count; i++) 15 { 16 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></tr>", 17 da.Rows[i]["ID"], da.Rows[i]["UserName"], da.Rows[i]["UserPass"],Convert.ToDateTime(da.Rows[i]["RegTime"]).ToShortDateString(), 18 da.Rows[i]["Email"],da.Rows[i]["ID"]); 19 } 20 string filePath = context.Request.MapPath("UserInfoList.html"); 21 string fileContent = File.ReadAllText(filePath); 22 fileContent= fileContent.Replace("$tbody",sb.ToString());//$tbody是html 里面的 23 context.Response.Write(fileContent); 24 } 25 } 26 }
在这里的html里面指定AddUserInfo.html
后续
时间: 2024-09-29 09:19:01