1.展示人员列表
htm文件:

<a href="PersonEditAddNew.ashx?action=addnew">添加</a>
</p>
<table border="1" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>用户名</th>
<th>密码</th>
<th>删除</th>
<th>编辑</th>
</tr>
</thead>
@personList
</table>

ashx文件:

/// <summary>
/// 展示数据库内容
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
DataTable table = SQLHelper.ExecuteReader("select * from user_table");
StringBuilder sb = new StringBuilder();
//将数据库的数据内容拼接到sb中
//编辑时将action和id提交到服务器是为了后续的判断选择(用context.Request["action"]可获取到提交的值)
foreach (DataRow row in table.Rows)
{
sb.Append("<tr><td>" + row["name"] + "</td><td>" + row["pwd"]
+ "</td><td><a onclick='return confirm(\"你真的要删除吗?\")' href='PersonDelete.ashx?id="
+ row["id"] + "'>删除</a></td>" +
"<td><a href='PersonEditAddNew.ashx?action=edit&id=" + row["id"] + "'>编辑</a></td></tr>");
}
//获取服务器上的html文件并转换成本地路径
string fileName = context.Server.MapPath("~/PersonList.htm");
//读取html内容
string html = File.ReadAllText(fileName);
//替换html部分内容
html = html.Replace("@personList", sb.ToString());
//展示内容
context.Response.Write(html);
}

2.删除人员
ashx文件:

/// <summary>
/// 删除数据库内容
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//获取提交给服务器的id值
int id = Convert.ToInt32(context.Request["id"]);
//执行SQL删除语句
SQLHelper.ExecuteNonQuery("delete from user_table where id=@id",
new SqlParameter { ParameterName = "@id", Value = id });
//重定向到PersonList
context.Response.Redirect("PersonList.ashx");
}

3.增加与修改
htm文件:

<head>
<title>@actionName</title>
</head>
<body>
<form action="PersonSave.ashx" method="post">
<input type="hidden" name="editAddNew" value="@action" />
<input type="hidden" name="id" value="@id" />
用户名:<input type="text" name="name" value="@name" />
密码:<input type="text" name="pwd" value="@pwd" />
</p>
<input type="submit" name="btnOK" value="保存" />
</form>
</body>

ashx文件:

/// <summary>
/// 编辑与新增人员
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//获取提交表单时action的值
string action = context.Request["action"];
//如果是编辑
if (action == "edit")
{
int id = Convert.ToInt32(context.Request["id"]);//获取提交表单时id的值
//根据id值获得name和pwd值
DataTable table = SQLHelper.ExecuteReader("select name,pwd from user_table where id=@id",
new SqlParameter { ParameterName = "@id", Value = id });
//逻辑判断,提高程序安全性
if (table.Rows.Count <= )
{
context.Response.Write("<font size='5' color='red'>没有找到id=" + id + "的人员</font>");
return;
}
if (table.Rows.Count > )
{
context.Response.Write("<font size='5' color='red'>找到了多条id=" + id + "的人员</font>");
return;
}
//值name和pwd存到DataRow中
DataRow row = table.Rows[];
string name = (string)row["name"];
string pwd = (string)row["pwd"];
//读取PersonEditAddNew.htm内容
string html = CommonHelper.ReadHtml("~/PersonEditAddNew.htm");
//将三个隐藏字段和name、pwd替换相应值,为编辑时将@action替换为editPerson
html = html.Replace("@actionName", "编辑人员").Replace("@action", "editPerson")
.Replace("@id",id.ToString()).Replace("@name", name).Replace("@pwd", pwd);
context.Response.Write(html);//输出替换后的值
}
//如果是新增
else if (action == "addnew")
{
string html = CommonHelper.ReadHtml("~/PersonEditAddNew.htm");
//将name和pwd替换成空值,为新增时将@action替换为addPerson
html = html.Replace("@actionName", "新增人员").Replace("@action", "addPerson")
.Replace("@name", "").Replace("@pwd", "");
context.Response.Write(html);
}
//加上else是为了当找不到action值,有人故意在网页地址栏中改变action的值
else
{
context.Response.Write("action错误");
}
}

4.保存人员
ashx文件:

/// <summary>
/// 保存新增或修改人员
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string editAddNew = context.Request["editAddNew"];
string name = context.Request["name"];
string pwd = context.Request["pwd"];
if (string.IsNullOrEmpty(name))
{
context.Response.Write("<font size='5' color='red'>姓名必填</fonr>");
return;
}
if (string.IsNullOrEmpty(pwd))
{
context.Response.Write("<font size='5' color='red'>密码必填</fonr>");
return;
}
//编辑时,在PersonEditAddNew.ashx中已将PersonEditAddNew.htm的隐藏字段@action替换为editPerson
if (editAddNew == "editPerson")
{
//此处获取到的id值是PersonEditAddNew.htm页面隐藏字段的id,并非GET请求的id值
int id = Convert.ToInt32(context.Request["id"]);
SQLHelper.ExecuteNonQuery("update user_table set name=@name,pwd=@pwd where id=@id",
new SqlParameter { ParameterName = "@name", Value = name },
new SqlParameter { ParameterName = "@pwd", Value = pwd },
new SqlParameter { ParameterName = "@id", Value = id });
context.Response.Redirect("PersonList.ashx");
}
//新增时,在PersonEditAddNew.ashx中已将PersonEditAddNew.htm的隐藏字段@action替换为addPerson
else if (editAddNew == "addPerson")
{
SQLHelper.ExecuteNonQuery("insert into user_table(name,pwd) values(@name,@pwd)",
new SqlParameter { ParameterName = "@name", Value = name },
new SqlParameter { ParameterName = "@pwd", Value = pwd });
context.Response.Redirect("PersonList.ashx");
}
{
context.Response.Write("<font size='5' color='red'>服务器出错</fonr>");
}
}

5.封装的类CommonHelper:

/// <summary>
/// 返回HTML文件内容
/// </summary>
/// <param name="fileName">HTML文件</param>
/// <returns></returns>
public static string ReadHtml(string fileName)
{
HttpContext context = HttpContext.Current;
string fullpath = context.Server.MapPath(fileName);
string html = File.ReadAllText(fullpath);
return html;
}

第一套增删改查(htm+ashx完成)的更多相关文章

  1. SSM整合_年轻人的第一个增删改查_基础环境搭建

    写在前面 SSM整合_年轻人的第一个增删改查_基础环境搭建 SSM整合_年轻人的第一个增删改查_查找 SSM整合_年轻人的第一个增删改查_新增 SSM整合_年轻人的第一个增删改查_修改 SSM整合_年 ...

  2. SSM整合_年轻人的第一个增删改查_查找

    写在前面 SSM整合_年轻人的第一个增删改查_基础环境搭建 SSM整合_年轻人的第一个增删改查_查找 SSM整合_年轻人的第一个增删改查_新增 SSM整合_年轻人的第一个增删改查_修改 SSM整合_年 ...

  3. SSM整合_年轻人的第一个增删改查_新增

    写在前面 SSM整合_年轻人的第一个增删改查_基础环境搭建 SSM整合_年轻人的第一个增删改查_查找 SSM整合_年轻人的第一个增删改查_新增 SSM整合_年轻人的第一个增删改查_修改 SSM整合_年 ...

  4. 实训第一天--增删改查加hibernate+搭建环境常见问题

    1.     搭建环境 安装 1)谷歌浏览器 2)jdk-7u67-windows-i586.exe 3)Tomcat7 4)NavicatforMySQL 两种方式: ftp://172.21.95 ...

  5. 使用 Spring Boot 搭建一套增删改查(无多余代码)

    前言 这是我学习 Spring Boot 的第三篇文章,终于可以见到效果了.错过的同学可以看看之前的文章 我们为什么要学习 Spring Boot Spring Boot 入门详细分析 在入门的基础上 ...

  6. 利用Servlet做一套增删改查

    真的,稳住,考上研,利用两年逆袭.一步一步来,实在不行,最后最差也不过就是就回家种地,想想也不错. 前期准备配置 建一个动态web项目 新建Dynamic Web ProjectFile->Ne ...

  7. BootstrapTable与KnockoutJS相结合实现增删改查功能

    http://www.jb51.net/article/83910.htm KnockoutJS是一个JavaScript实现的MVVM框架.通过本文给大家介绍BootstrapTable与Knock ...

  8. JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(一)

    前言:出于某种原因,需要学习下Knockout.js,这个组件很早前听说过,但一直没尝试使用,这两天学习了下,觉得它真心不错,双向绑定的机制简直太爽了.今天打算结合bootstrapTable和Kno ...

  9. linq的简单增删改查

    Linq高集成化的数据访问类,它会自动映射数据库结构,将表名完整映射成为类名,将列名完整映射成字段名数据库数据访问,能大大减少代码量.(反正最后结果就是不用写ado.Net那一套增删改查,有一套封装好 ...

随机推荐

  1. UVA 11527 Unique Snowflakes

    用STL做会很方便 SET: /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring ...

  2. Intel 80x86 Linux Kernel Interrupt(中断)、Interrupt Priority、Interrupt nesting、Prohibit Things Whthin CPU In The Interrupt Off State

    目录 . 引言 . Linux 中断的概念 . 中断处理流程 . Linux 中断相关的源代码分析 . Linux 硬件中断 . Linux 软中断 . 中断优先级 . CPU在关中断状态下编程要注意 ...

  3. jsp学习(二)

    jsp运行原理 当服务器上的一个jsp页面被第一次请求标记时,服务器上的jsp引擎首先将jsp页面文件转译成一个Java文件,并编译这个java文件生成字节码文件,然后执行字节码文件响应客户的请求. ...

  4. 修改MYSQL 表中的字段属性

    1.登录数据库 >mysql -u root -p 数据库名称 2.查询所有数据表 >show tables; 3.查询表的字段信息 >desc 表名称; 4.1.修改某个表的字段类 ...

  5. another app is currently holding the yum lock;waiting for it to exit解决

    有时用yum升级一些文件时,会出现以下情况:   another app is currently holding the yum lock;waiting for it to exit...   可 ...

  6. JS 瀑布流布局

    瀑布流布局 HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  7. html5浮动、等高、弹性盒模型

    1px dashed虚线 box-sizing拯救了布局 1.inherit  继承父级 2.content-box(默认)-----这个盒子的边框.内边距 这2个值是不包括在width和height ...

  8. juery动态添加和删除

    拼语句添加框(不能删除原有的tr) //点击a标签 $("#a").on("click",function(){ var $newtr = $("&l ...

  9. [转载]Using ngOptions In AngularJS

    http://odetocode.com/blogs/scott/archive/2013/06/19/using-ngoptions-in-angularjs.aspx?utm_source=tui ...

  10. javascript单例模式的理解

    javascript单例模式的理解 阅读目录 理解单例模式 使用代理实现单例模式 理解惰性单例 编写通用的惰性单例 单例模式使用场景 回到顶部 理解单例模式 单例模式的含义是: 保证一个类只有一个实例 ...