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. 获取本机IP_考虑多网卡的情况

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  2. linux c学习笔记----进程创建(fork,wait,waitpid)

    1.pid_t fork(); (1)当一个进程调用了fork 以后,系统会创建一个子进程.这个子进程和父进程不同的地方只有他的进程ID 和父进程ID,其他的都是一样.就象符进程克隆(clone)自己 ...

  3. ActionBar右边菜单按钮的添加

    在res目录下新建文件夹menu,存放men.xml文件 menu.xml <menu xmlns:android="http://schemas.android.com/apk/re ...

  4. 安装hadoop2.4.0遇到的问题

    一.执行start-dfs.sh后,datenode没有启动 查看日志如下: 2014-06-18 20:34:59,622 FATAL org.apache.hadoop.hdfs.server.d ...

  5. 【MVC5】ASP.NET MVC 项目笔记汇总

    ASP.NET MVC 5 + EntityFramework 6 + MySql 先写下列表,之后慢慢补上~ 对MySql数据库使用EntityFramework 使用域用户登录+记住我 画面多按钮 ...

  6. SQL Server 2005 数据库复制(转载)

    对于一个地域分散的大型企业组织来说,构建具有典型的分布式计算机特征的大型企业管理信息系统时,总要解决一个很重要的问题:如何在多个不同数 据库服务器之间保证共享数据的一致性.之所以有这个重要的问题在于企 ...

  7. JavaScriptSerializer中日期序列化问题解决方案

    JavaScriptSerializer中日期序列化问题解决方案 直接进入主题: class Student { public int age { get; set; } public DateTim ...

  8. 新浪微博客户端(3)-封装UIBarButtonItem

    单独给NavigationBar上的两个NavigationItem设置图片显得比较麻烦,下面对创建单个UIBarButtonItem的过程进行封装. UIBarButtonItem+Extensio ...

  9. Java-java中的有符号,无符号操作以及DataInputStream

    1. 无符号和有符号 计算机中用补码表示负数,并且有一定的计算方式:另外,用二进制的最高位表示符号,0表示正数.1表示负数.这种说法本身没错,可是要有一定的解释,不然它就是错的,至少不能解释,为什么字 ...

  10. linux下的视频音频播放器终极解决方案

    要使用(启用)rpmfusion, 一定要先启用enable epel包: Important notes You need to enable EPEL on RHEL 5 & 6 or c ...