Asp_CRUD
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
public class CRUD_main : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
DataTable td= SqlHelper.ExcteQuery("select * from T_persons");//
StringBuilder sb = new StringBuilder();
foreach(DataRow rows in td.Rows)
{
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("'>编辑");
sb.Append("</td></tr>"); }
string CRUD_main =Httphelper.virtualPath("~/CRUD_main.html");
string CRUD_main_ = CRUD_main.Replace("@table",sb.ToString()).Replace("@title","主页面").Replace("@SET","T_persons");
context.Response.Write(CRUD_main_);//替换后的结果
}
经过服务器的解析返回给浏览器的头解析的信息。
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
context.Response.Write(CRUD_main_);// 服务器返回给浏览器的这段HTML代码。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>主页面</title>
<style type="text/css">
.table
{
width:300px;
}
.table tr td
{
border-top:#FF0000 solid 4px;
width:20%;
background-color:ghostwhite;
text-align:center;
line-height:24px;
}
</style>
</head>
<body>
<h1>. . T_persons</h1> <a href ="CRUD_addNew.ashx?action=add">添加</a>
<table class="table">
<br/>
<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> </table>
</body>
</html>
浏览器自己解析得到下面内容展示

<a href = 'CRUD_delete.ashx?id= @Id'>删除
点击删除按钮,由HTML 代码可以看出 , 删除按钮这个超链接跳转到了CRUD_delete.ashx 这个文件中。后面的id 是遍历得到。
浏览器在一次像服务器IIS 请求。CRUD_delete.ashx?id=@id.
服务器在拿到这个请求后。开始执行CRUD_delete.ashx.cs这段代码。
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
int id = Convert.ToInt32( context.Request["id"]);
SqlHelper.ExcuteNonQuery("delete from T_persons where id =@id",new SqlParameter("@id",id));
context.Response.Redirect("CRUD_main.ashx");
}
从代码看出这个是对数据库进行删除操作的。 删除完之后会重新定向会CRUD_main.ashx。进行一次新的查询。
由浏览器的F12 追踪一下可以看到。

当点击添加的时候,根据HTML代码可以看出是跳转到了
CRUD_addNew.ashx?action=add
并且给传了一个值。action=add/, 服务器拿到这个request(请求) 开始执行以下代码。
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
// CRUD_addNew.ashx?action=add
// CRUD_addNew.ashx?action =edit&id=1
string addNewHtml = Httphelper.virtualPath("~/CRUD_addNew.html");
string action = context.Request["action"];
if (action == "add")
{
addNewHtml = addNewHtml.Replace("@title","新增").Replace("@action",action).Replace("@name", "").Replace("@password", "").Replace("@Age","");
context.Response.Write(addNewHtml);
}
else
{
Httphelper.OutPutErr("Action错误");
}
}
服务器根据判断,返回了一个添加页面的HTML代码给浏览器。
| <!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 代码如下 为看清楚,删除了判断数据合法性的代码。
public class CURD_Save : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
// 判断提交过来的是新增还是修改
string action =context.Request["action"];
string name = context.Request["name"];
string Password = context.Request["password"];
string strAge = context.Request["Age"]; int Age = Convert.ToInt32(strAge); if (action == "add")
{ SqlHelper.ExcuteNonQuery("INSERT INTO T_persons (UserName,password,age)values(@name,@password,@age)",
new SqlParameter("@age", Age), new SqlParameter("@name", name), new SqlParameter("@password", Password));
HttpContext.Current.Response.Redirect("CRUD_main.ashx"); }
else if (action == "edit") // 编辑
{ int id = Convert.ToInt32(context.Request["id"]);
SqlHelper.ExcuteNonQuery(" UPDATE T_Persons Set Age=@age,UserName=@name ,password=@password where id =@id",
new SqlParameter("@age", Age),new SqlParameter("@name",name),new SqlParameter("@password",Password),new SqlParameter("@id",id));
HttpContext.Current.Response.Redirect("CRUD_main.ashx"); }
else
{
Httphelper.OutPutErr("Action错误");
} }
根据action传过来的值,判断是添加还是修改。 执行对应的SQL语句。 然后重新定向回主页面
Asp_CRUD的更多相关文章
随机推荐
- Light oj 1214-Large Division (同余定理)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我 ...
- 图文讲解:iOS App提交流程
原文:http://www.toceansoft.com/ios/3287.jhtml 一.证书的导出 1.1.前期工作 首先你需要有一个苹果的开发者帐号,一个Mac系统. 如果没有帐号可以在打开ht ...
- Schwarz导数与凹凸性
命题 1: 定义区间$I$上的Schwarz导数$$D^{2}f(x)=\lim_{h\to 0}\frac{f(x+h)+f(x-h)-2f(x)}{h^{2}}$$若$D^{2}f(x)\geq ...
- 用C++ 自娱自乐
最无聊的时光当属 考试前的复习时段了,在一些论坛上看到一些用字符组成的图像,觉得有点意思,于是,自己 用C++ 参考一些论坛的图像,写了下面这个东西,来表达此时的心情. #include<ios ...
- GitHub托管项目步骤
1.打开Git Shell ,进入你要托管的项目目录里.然后输入git init ,该项目下就会多一个.git文件夹 2.点击add,然后再path里面输入你项目的,git文件夹目录地址.如下: 3. ...
- maven使用.01.Hello World
要说Java世界有什么东西是我最为留恋的:在写其他语言程序的时候,我最为想要的东西,那非maven莫属. 什么是Maven? Maven能做什么? Maven是一个针对Java的自动构建工具.所谓自动 ...
- Codeforces Gym 100500F Problem F. Door Lock 二分
Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...
- Codeforces Gym 100513M M. Variable Shadowing 暴力
M. Variable Shadowing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/ ...
- CopyU!v2 已经收录到腾讯软件管家!
腾讯软件管家已经正式收录了CopyU!,这样大家又多了一个安全快速下载软件的好途径!腾讯渠道的CopyU!更新将会保持与官方同步,现有下载CopyU!软件的渠道中,官方保证同步的主要有: 1.非凡软件 ...
- iOS开发——UI_swift篇&TableView实现页眉和页脚
TableView实现页眉和页脚 在UItableView中header和footer是很常见的,而且他能让你实现很复杂的功能,我们见过最多的就是下拉刷新和上啦加载更多,当然你还可以在上面添加一个 ...