第一套增删改查(htm+ashx完成)
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完成)的更多相关文章
- SSM整合_年轻人的第一个增删改查_基础环境搭建
写在前面 SSM整合_年轻人的第一个增删改查_基础环境搭建 SSM整合_年轻人的第一个增删改查_查找 SSM整合_年轻人的第一个增删改查_新增 SSM整合_年轻人的第一个增删改查_修改 SSM整合_年 ...
- SSM整合_年轻人的第一个增删改查_查找
写在前面 SSM整合_年轻人的第一个增删改查_基础环境搭建 SSM整合_年轻人的第一个增删改查_查找 SSM整合_年轻人的第一个增删改查_新增 SSM整合_年轻人的第一个增删改查_修改 SSM整合_年 ...
- SSM整合_年轻人的第一个增删改查_新增
写在前面 SSM整合_年轻人的第一个增删改查_基础环境搭建 SSM整合_年轻人的第一个增删改查_查找 SSM整合_年轻人的第一个增删改查_新增 SSM整合_年轻人的第一个增删改查_修改 SSM整合_年 ...
- 实训第一天--增删改查加hibernate+搭建环境常见问题
1. 搭建环境 安装 1)谷歌浏览器 2)jdk-7u67-windows-i586.exe 3)Tomcat7 4)NavicatforMySQL 两种方式: ftp://172.21.95 ...
- 使用 Spring Boot 搭建一套增删改查(无多余代码)
前言 这是我学习 Spring Boot 的第三篇文章,终于可以见到效果了.错过的同学可以看看之前的文章 我们为什么要学习 Spring Boot Spring Boot 入门详细分析 在入门的基础上 ...
- 利用Servlet做一套增删改查
真的,稳住,考上研,利用两年逆袭.一步一步来,实在不行,最后最差也不过就是就回家种地,想想也不错. 前期准备配置 建一个动态web项目 新建Dynamic Web ProjectFile->Ne ...
- BootstrapTable与KnockoutJS相结合实现增删改查功能
http://www.jb51.net/article/83910.htm KnockoutJS是一个JavaScript实现的MVVM框架.通过本文给大家介绍BootstrapTable与Knock ...
- JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(一)
前言:出于某种原因,需要学习下Knockout.js,这个组件很早前听说过,但一直没尝试使用,这两天学习了下,觉得它真心不错,双向绑定的机制简直太爽了.今天打算结合bootstrapTable和Kno ...
- linq的简单增删改查
Linq高集成化的数据访问类,它会自动映射数据库结构,将表名完整映射成为类名,将列名完整映射成字段名数据库数据访问,能大大减少代码量.(反正最后结果就是不用写ado.Net那一套增删改查,有一套封装好 ...
随机推荐
- dto
dto dto- datatransfer object(数据传输对象):dto在设计之初的主要考量是以粗粒度的数据结构减少网络通信并简化调用接口. http://www.cnblogs.com/wu ...
- 如何在ECSHOP前台后台中讲版权内容清除
如何在ECSHOP前台后台中讲版权内容清除 作者:河南电脑学校网 点击次数:1065 发布时间:2012-02-02 20:13:58 一.ECSHOP前台显示的页面的版权在下面几个地方修改:(本人不 ...
- Spring MVC 读取静态资源时404错误
背景:web.xml配置时拦截策略是拦截所有请求: <servlet> <servlet-name>springmvc</servlet-name> <ser ...
- dedecms首页调用栏目内容和单页内容的方法
常用的需要调到首页来的单页内容,比如企业简介.联系我们等等内容,我们在首页可能都要进行体现.通过常规的方式,包括查阅dede官方论坛资料,都找不到比较合适的答案.今天我们就提供两种方式进行调用. 我们 ...
- 常用webshell提权方法总结
pcAnywhere提权:1.利用pcAnywhere提权,前提条件是pcAnywhere默认安装到默认路径,以及它的目录安全权限有users权限,如果管理员删除了users和power users用 ...
- java ssl https 连接详解 生成证书
我们先来了解一下什么理HTTPS 1. HTTPS概念 1)简介 HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全 ...
- C++构造函数详解及显式调用构造函数
来源:http://www.cnblogs.com/xkfz007/archive/2012/05/11/2496447.html c++类的构造函数详解 ...
- Ubuntu技巧之 is not in the sudoers file解决方法
转自:http://www.linuxidc.com/Linux/2010-12/30386.htm 1)进入到root用户下. 2)添加文件的写权限.也就是输入命令"chmod u+w / ...
- SSRS匿名访问
---本人数据库是SqlServer2008 R2 匿名访问Reporting Service 2008 我想通过访问Url的方式,把部署到Sql Server Reporting Service ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...