Asp_增删改查。逻辑流程

启动服务器。 地址为127.0.0.1 端口为随机分配 2607
然后在浏览器中输入http://localhost:2670/CRUD_main.ashx
浏览器像服务器发送这样一组信息

浏览器请求的头文件
GET /CRUD_main.ashx HTTP/1.1   
Host: localhost:2670
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8

服务器获取到请求 CRUD_main.ashx
服务器端开始执行这段代码。 CRUD_main.ashx.cs

  1. public class CRUD_main : IHttpHandler
  2. {
  3.  
  4. public void ProcessRequest(HttpContext context)
  5. {
  6. DataTable td= SqlHelper.ExcteQuery("select * from T_persons");//
  7. StringBuilder sb = new StringBuilder();
  8. foreach(DataRow rows in td.Rows)
  9. {
  10. sb.Append("<tr><td>").Append(rows["UserName"]).Append("</td><td>").Append(rows["password"]).Append("</td><td>").Append(rows["Age"]).Append("</td><td>").Append("<a href = 'CRUD_delete.ashx?id= ").Append(rows["id"]).Append("'>删除").Append("</td><td>").Append("<a href ='CRUD_addNew.ashx?action=edit&id=").Append(rows["id"]).Append("'>编辑");
  11. sb.Append("</td></tr>");
  12.  
  13. }
  14. string CRUD_main =Httphelper.virtualPath("~/CRUD_main.html");
  15. string CRUD_main_ = CRUD_main.Replace("@table",sb.ToString()).Replace("@title","主页面").Replace("@SET","T_persons");
  16. context.Response.Write(CRUD_main_);//替换后的结果
  17. }

经过服务器的解析返回给浏览器的头解析的信息。

HTTP/1.1 200 OK  
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcQ19zaGFycF9DbGFzc1xXZWJfQXNwXGFzcF/lop7liKDmlLnmn6VcQ1JVRF9tYWluLmFzaHg=?=
X-Powered-By: ASP.NET
Date: Sun, 08 May 2016 15:44:36 GMT
Content-Length: 702

  1. context.Response.Write(CRUD_main_);// 服务器返回给浏览器的这段HTML代码。
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <title>主页面</title>
  6. <style type="text/css">
  7. .table
  8. {
  9. width:300px;
  10. }
  11. .table tr td
  12. {
  13. border-top:#FF0000 solid 4px;
  14. width:20%;
  15. background-color:ghostwhite;
  16. text-align:center;
  17. line-height:24px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <h1>. . T_persons</h1>
  23.  
  24. <a href ="CRUD_addNew.ashx?action=add">添加</a>
  25. <table class="table">
  26. <br/>
  27. <tbody><th>姓名</th><th>密码</th><th>年龄</th><tr><td>welzek</td><td>loves</td><td>88</td><td><a href = 'CRUD_delete.ashx?id= 15'>删除</td><td><a href ='CRUD_addNew.ashx?action=edit&id=15'>编辑</td></tr><tr><td>welzek</td><td>loves</td><td>88</td><td><a href = 'CRUD_delete.ashx?id= 16'>删除</td><td><a href ='CRUD_addNew.ashx?action=edit&id=16'>编辑</td></tr><tr><td>welzek</td><td>loves</td><td>88</td><td><a href = 'CRUD_delete.ashx?id= 17'>删除</td><td><a href ='CRUD_addNew.ashx?action=edit&id=17'>编辑</td></tr></tbody>
  28.  
  29. </table>
  30. </body>
  31. </html>

浏览器自己解析得到下面内容展示

  1. <a href = 'CRUD_delete.ashx?id= @Id'>删除

点击删除按钮,由HTML 代码可以看出 ,  删除按钮这个超链接跳转到了CRUD_delete.ashx 这个文件中。后面的id 是遍历得到。

浏览器在一次像服务器IIS 请求。CRUD_delete.ashx?id=@id.

服务器在拿到这个请求后。开始执行CRUD_delete.ashx.cs这段代码。

  1. public void ProcessRequest(HttpContext context)
  2. {
  3. context.Response.ContentType = "text/html";
  4. int id = Convert.ToInt32( context.Request["id"]);
  5. SqlHelper.ExcuteNonQuery("delete from T_persons where id =@id",new SqlParameter("@id",id));
  6. context.Response.Redirect("CRUD_main.ashx");
  7. }

从代码看出这个是对数据库进行删除操作的。 删除完之后会重新定向会CRUD_main.ashx。进行一次新的查询。

由浏览器的F12 追踪一下可以看到。

当点击添加的时候,根据HTML代码可以看出是跳转到了

  1. CRUD_addNew.ashx?action=add

并且给传了一个值。action=add/,  服务器拿到这个request(请求) 开始执行以下代码。

  1. public void ProcessRequest(HttpContext context)
  2. {
  3. context.Response.ContentType = "text/html";
  4. // CRUD_addNew.ashx?action=add
  5. // CRUD_addNew.ashx?action =edit&id=1
  6. string addNewHtml = Httphelper.virtualPath("~/CRUD_addNew.html");
  7. string action = context.Request["action"];
  8. if (action == "add")
  9. {
  10. addNewHtml = addNewHtml.Replace("@title","新增").Replace("@action",action).Replace("@name", "").Replace("@password", "").Replace("@Age","");
  11. context.Response.Write(addNewHtml);
  12. }
  13. else
  14. {
  15. Httphelper.OutPutErr("Action错误");
  16. }
  17. }

服务器根据判断,返回了一个添加页面的HTML代码给浏览器。

  1.  
  <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>新增人员</title>
  </head>
  <body>
  <
  <form action="CURD_Save.ashx" method="get">
  <input type="hidden" name ="id" value="@id" />
  <input type="hidden" name="action" value="add" />
  姓名<input type="text" name="name" value=""/>
  密码<input type="text" name="password" value=""/>
  年龄<input type="text" name="Age" value=""/>
  <input type="submit" value="提交" />
  </form>
  </body>
  </html>
 

浏览器解析后得到。下面这个页面。

为了方便查看,post 改为get

由form 表单看出  把这些数据提交到了 CURD_Save.ashx这个页面中。CRUD_Save.ashx 代码如下 为看清楚,删除了判断数据合法性的代码。

  1. public class CURD_Save : IHttpHandler
  2. {
  3.  
  4. public void ProcessRequest(HttpContext context)
  5. {
  6. context.Response.ContentType = "text/html";
  7. // 判断提交过来的是新增还是修改
  8. string action =context.Request["action"];
  9. string name = context.Request["name"];
  10. string Password = context.Request["password"];
  11. string strAge = context.Request["Age"]; int Age = Convert.ToInt32(strAge);
  12.  
  13. if (action == "add")
  14. {
  15.  
  16. SqlHelper.ExcuteNonQuery("INSERT INTO T_persons (UserName,password,age)values(@name,@password,@age)",
  17. new SqlParameter("@age", Age), new SqlParameter("@name", name), new SqlParameter("@password", Password));
  18. HttpContext.Current.Response.Redirect("CRUD_main.ashx");
  19.  
  20. }
  21. else if (action == "edit") // 编辑
  22. {
  23.  
  24. int id = Convert.ToInt32(context.Request["id"]);
  25. SqlHelper.ExcuteNonQuery(" UPDATE T_Persons Set Age=@age,UserName=@name ,password=@password where id =@id",
  26. new SqlParameter("@age", Age),new SqlParameter("@name",name),new SqlParameter("@password",Password),new SqlParameter("@id",id));
  27. HttpContext.Current.Response.Redirect("CRUD_main.ashx");
  28.  
  29. }
  30. else
  31. {
  32. Httphelper.OutPutErr("Action错误");
  33. }
  34.  
  35. }

根据action传过来的值,判断是添加还是修改。 执行对应的SQL语句。  然后重新定向回主页面

Asp_CRUD的更多相关文章

随机推荐

  1. freemaker遍历嵌套list的map

    <#if disMap?exists> <#list disMap?keys as key> <#if ((disMap[key]))??&&((disM ...

  2. c# 使用ChartDirector绘图的一些个人体会

    c# 使用ChartDirector绘图的一些个人体会 引言:       最近给客户做一个B/S架构的证券交易大屏幕软件,其中用到对证券指数的走势以及成交量的图形绘制,由于客户要求图形的清晰而且要做 ...

  3. struts validate

    1  login.jsp方式1 <%@ page language="java" import="java.util.*" pageEncoding=&q ...

  4. Android BLE开发之Android手机搜索iBeacon基站

    本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 上次讲了Android手机与BLE终端之间的通信,而最常见的BLE终端应该是苹果公司倡导的iBeacon基站. ...

  5. 从零开始学android开发-通过WebService获取今日天气情况

    因为本身是在搞.NET方面的东东,现在在学习Android,所以想实现Android通过WebService接口来获取数据,网上很多例子还有有问题的.参考:Android 通过WebService进行 ...

  6. Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索

    D. Wilbur and Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...

  7. C# 利用范型与扩展方法重构代码

    在一些C#代码中常常可以看到 //An Simple Example By Ray Linn class CarCollection :ICollection { IList list; public ...

  8. Android-WizardPager

    https://github.com/HeinrichReimer/Android-WizardPager

  9. android应用程序监听SMS Intent广播

    当设备接收到一条新的SMS消息时,就会广播一个包含了android.provider.Telephony.SMS_RECEIVED动作的Intent. 对于应用程序监听SMS Intent广播,首先需 ...

  10. session销毁

    session.invalidate(),session.invalidate的销毁是把这个session所带的用户彻底的销毁,这个session跟用户已经紧密联合在一起,所以就一起销毁了,这样就算换 ...