一般处理程序增删改

一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩展名。一个httpHandler接受并处理一个http请求,类比于Java中的servlet。类比于在Java中需要继承HttpServlet类。在net中需要实现IHttpHandler接口,这个接口有一个IsReusable成员,一个待实现的方法ProcessRequest(HttpContextctx) 。程序在processRequest方法中处理接受到的Http请求。成员IsReusable指定此IhttpHnadler的实例是否可以被用来处理多个请求。
 
.ashx程序适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。

IHttpHandler借口中的ProcessRequest(后面简称PR方法),如图1-1:
 
 IHttpHandler接口中的ProcessRequest
        图1-1
一般处理程序中的指令集,如图1-2:
指令集会找到对应的一般处理程序。
 
一般处理程序中的指令集
            图1-2
一般处理程序,如图1-3:
 一般处理程序
        图1-3

 业务:
利用ashx实现增删改,文件内容如图2-1:
ashx增删改文件内容
    图2-1
    
  
  
分析:
ashx中前段代码需要用html也来写,这些html页称为模板页。
模板页中的后台数据显示,暂时用${tr}之类的占位符来表示
然后在一般处理程序中通过获取模板页,替换占位符,响应输出内容
 
模板中的占位符,如图2-2:
模板中的占位符
  

源代码:
GetList.ashx.cs
 1 public void ProcessRequest(HttpContext context)
 2         {
 3             DataTable dt = DbHelperSQL.GetDataTable("select * from ContactInfo where isdelete=0");
 4 
 5 
 6             //找到模板页所对应的路径
 7             string path = context.Server.MapPath("/Templ/GetList.html");
 8             //读取模板页面里面的内容
 9             string str = System.IO.File.ReadAllText(path);
10 
11             //遍历dt,生成sb
12             System.Text.StringBuilder sb = new System.Text.StringBuilder(200);
13             foreach (DataRow item in dt.Rows)
14             {
15                 sb.Append("<tr>");
16                 sb.Append("<td>" + item["ID"] + "</td>");
17                 sb.Append("<td>" + item["ContactName"] + "</td>");
18                 sb.Append("<td>" + item["CommonMobile"] + "</td>");
19                 sb.Append("<td>" + item["GroupId"] + "</td>");
20                 sb.Append("<td>" + "<a href='javascript:void(0)' onclick='del(" + item["ID"] + ")' >删</a> | <a href='edit.ashx?id=" + item["ID"] + "'>修改</a>" + "</td>");
21                 sb.Append("</tr>");
22             }
23             //把模板页里面的点位替换
24             str = str.Replace("${trs}", sb.ToString());
25 
26             //输出内容
27             context.Response.Write(str);
28         }
 1  public void ProcessRequest(HttpContext context)
 2         {
 3             //1.0接收传递过来的参数
 4             string id = context.Request.QueryString["id"];
 5             //2.0执行删除操作
 6             int i = DbHelperSQL.ExecuteSql("update ContactInfo set IsDelete=1 where id=" + id);
 7             //3.0判断是否删除成功
 8             if (i > 0)
 9             {
10                 context.Response.Write("<script>alert('删除成功');window.location='GetList.ashx'</script>");
11             }
12             else
13             {
14                 context.Response.Write("<script>alert('删除失败');window.location='GetList.ashx'</script>");
15             }
16         }
del.ashx.cs
 1  public void ProcessRequest(HttpContext context)
 2         {
 3             //得到当前请求的方式
 4             string method = context.Request.HttpMethod.ToLower();
 5             //判断当前请求是否是get
 6             if (method == "get")
 7             {
 8                 //得到get过来的参数
 9                 string id = context.Request.QueryString["id"];
10                 //去数据库里面查找出id对应的数据
11                 DataTable dt = DbHelperSQL.GetDataTable("select * from ContactInfo where ID=" + id);
12                 //去读取模板页
13                 string path = context.Server.MapPath("/Templ/edit.html");
14                 string str = System.IO.File.ReadAllText(path);
15                 //替换点位符
16                 str = str.Replace("${name}", dt.Rows[0]["ContactName"].ToString()).Replace("${phone}", dt.Rows[0]["CommonMobile"].ToString()).Replace("${id}", dt.Rows[0]["ID"].ToString());
17                 //将str返回给浏览器 
18                 context.Response.Write(str);
19             }
20             if (method == "post")
21             {
22                 //得到Post传递过来 的参数
23                 string name = context.Request.Form["name"];
24                 string phone = context.Request.Form["phone"];
25                 string id = context.Request.Form["id"];
26                 //把新的数据更新到数据库里面去
27                 int i = DbHelperSQL.ExecuteSql("update ContactInfo set ContactName='" + name + "',CommonMobile ='" + phone + "' where id=" + id);
28                 if (i > 0)
29                 {
30                     context.Response.Write("<script>alert('修改成功');window.location='GetList.ashx'</script>");
31                 }
32                 else
33                 {
34                     context.Response.Write("<script>alert('修改失败');window.location='GetList.ashx'</script>");
35                 }
36             }
37 
38         }
edit.ashx.cs

 

 
  

posted on 2014-07-07 20:44  两宝程序cboii  阅读(268)  评论(0)    收藏  举报

导航