删除数据库中的一条数据----》续
该操作实现:在删除数据后输出------》“再过n秒跳转到列表页面”
JSModel.htm文件:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <title></title> 6 7 <script type="text/javascript"> 8 @JSCode 9 </script> 10 </head> 11 <body> 12 <div id ="Msg"></div> 13 </body> 14 </html>
使用模板,然后将数据替换到模板中,避免重复输入
list.ashx页面内容不变。。。。
del.ashx页面内容变化部分特别显示:
1 <%@ WebHandler Language="C#" Class="del" %> 2 3 using System; 4 using System.Web; 5 using System.Data; 6 using System.Data.SqlClient; 7 8 public class del : IHttpHandler { 9 10 public void ProcessRequest (HttpContext context) { 11 //context.Response.ContentType = "text/plain"; 12 //context.Response.Write("Hello World"); 13 string JSstr = @" 14 var timecount=3; 15 function DoAsk(){ 16 if(timecount>0) 17 { 18 document.getElementById('Msg').innerHTML='删除成功,再过'+timecount+'秒跳转到列表页面'; 19 timecount=timecount-1; 20 setTimeout('DoAsk()',1000); 21 } 22 else 23 { 24 window.location='list.ashx'; 25 } 26 27 28 } 29 30 window.onload=function(){ DoAsk();} 31 "; 32 string path = context.Server.MapPath("JSModel.htm"); 33 string JSHtml = System.IO.File.ReadAllText(path); 34 JSHtml = JSHtml.Replace("@JSCode",JSstr); 35 36 string sid = context.Request.QueryString["id"]; 37 if (sid != null) 38 { 39 int ID = -1; 40 if (int.TryParse(sid, out ID)) 41 { 42 if (DoDel(ID)) 43 { 44 //context.Response.Write("删除成功!"); 45 context.Response.Write(JSHtml); 46 } 47 else 48 { 49 context.Response.Write("删除失败!"); 50 } 51 } 52 else 53 { 54 context.Response.Write("主键转换失败...."); 55 } 56 } 57 else 58 { 59 context.Response.Write("未获取主键值!"); 60 } 61 //context.Response.Redirect("List.ashx"); 62 } 63 private bool DoDel(int id) 64 { 65 string connStr = "Data Source=PC--20130405SCI\\YAOSIR;Initial Catalog=Clients;User ID= sa;Password=123456"; 66 SqlConnection conn = new SqlConnection(connStr); 67 conn.Open(); 68 string sqlStr = "delete from OrderClient where ClientID = @sid"; 69 SqlCommand cmd = new SqlCommand(sqlStr, conn); 70 SqlParameter par = new SqlParameter("@sid",id); 71 cmd.Parameters.Add(par); 72 int result = cmd.ExecuteNonQuery(); 73 74 return result>0; 75 } 76 77 public bool IsReusable { 78 get { 79 return false; 80 } 81 } 82 83 }