ASP.NET的一般处理程序对数据的基本操作
TableList.ashx:
<%@ WebHandler Language="C#" Class="TableList" %> using System;
using System.Web; public class TableList : IHttpHandler
{
private BLL.TransferExecuteAction execute = new BLL.TransferExecuteAction();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; string con = System.IO.File.ReadAllText(CommonHelper.GetPhysicsPathOfFile(context, "DataList.html"));
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (Model.TableModel item in execute.ExecuteDataTable())
{
int id = item.id;
sb.Append("<tr>");
sb.Append("<td>" + item.id + "</td>");
sb.Append("<td>" + item.课程编号 + "</td>");
sb.Append("<td><img src='" + item.图书封面 + "' alt=''/></td>");
sb.Append("<td>" + item.课程名称 + "</td>");
sb.Append("<td>" + item.学分 + "</td>");
sb.Append("<td><a href='javascript:Delete(" + id + ")'>删</a> <a href='Modify.ashx?id=" + id + "'>改</a></td>");
sb.Append("</tr>");
}
sb.Append("<tr><td colspan='6'><div style='float:right'><a href='Add.ashx'>增</a> </div></td></tr>");
context.Response.Write(con.Replace("{@trs}", sb.ToString()));
} public bool IsReusable
{
get
{
return false;
}
} }
DataList.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>数据列表</title>
<style type="text/css">
tr td {
padding: 3px;
margin: 3px auto;
}
</style>
<script type="text/javascript">
function Delete(id) {
if (confirm("确定要删除吗?")) {
window.location = 'Delete.ashx?id=' + id;//跳转到删除页面
}
};
</script>
</head>
<body>
<center>
<h3>表单数据</h3>
<table border="1">
<tr>
<td>id</td>
<td>课程编号</td>
<td>图书封面</td>
<td>课程名称</td>
<td>学分</td>
<td>操作</td>
</tr>
{@trs}
</table>
</center>
</body>
</html>
Add.ashx:
<%@ WebHandler Language="C#" Class="Add" %> using System;
using System.Web; public class Add : IHttpHandler
{
private BLL.TransferExecuteAction execute = null;
private Model.TableModel model = null;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//首次加载模版。
string emp = context.Request.Form["IsPostBack"], msg = string.Empty;
//是否点击提交按钮
if (!string.IsNullOrEmpty(emp))
{
model = new Model.TableModel();
model.课程编号 = context.Request.Form["num"];
model.课程名称 = context.Request.Form["name"];
model.图书封面 = context.Request.Form["face"];
int gra = ;
int.TryParse(context.Request.Form["grade"], out gra);
model.学分 = (int?)gra;
execute = new BLL.TransferExecuteAction();
if ( == execute.ExecuteAddData(model))
msg = "数据添加成功";
else
msg = "数据添加失败";
context.Response.Write(" <script>var time = 3;document.write('" + msg + ",<span id=\"time\" style=\"font-size:25px;color:red;\">3</span> 秒后自动跳转到数据列表页面。');setInterval(function () {if (time > 0) {time--;document.getElementById(\"time\").innerHTML = time;}else {window.location = 'TableList.ashx';}}, 1000);</script>");
}
else
{
string cont = System.IO.File.ReadAllText(CommonHelper.GetPhysicsPathOfFile(context, "OperationModel.html"));
cont = cont.Replace("{@Operation}", "增加").Replace("{@id}", "").Replace("{@action}", "Add.ashx").Replace("{@num}", "")
.Replace("{@face}", "").Replace("{@name}", "").Replace("{@grade}", "");
context.Response.Write(cont);
}
} public bool IsReusable
{
get
{
return false;
}
} }
Delete.ashx:
<%@ WebHandler Language="C#" Class="Delete" %> using System;
using System.Web; public class Delete : IHttpHandler
{
private BLL.TransferExecuteAction execute = null; public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string msg = string.Empty;
execute = new BLL.TransferExecuteAction();
//是否点击提交按钮
int id = int.Parse(context.Request.QueryString["id"]);
execute.ExecuteDeleteDataById(id);
if ( == )
{
msg = "数据删除成功";
}
else
{
msg = "数据删除失败";
}
context.Response.Write(" <script type=\"text/javascript\">var time = 3;document.write('" + msg + ",<span id=\"time\" style=\"font-size:25px;color:red;\">3</span> 秒后自动跳转到数据列表页面。');setInterval(function () {if (time > 0) {time--;document.getElementById(\"time\").innerHTML = time;}else {window.location = 'TableList.ashx';}}, 1000);</script>");
} public bool IsReusable
{
get
{
return false;
}
} }
Modify.ashx:
<%@ WebHandler Language="C#" Class="Modify" %> using System;
using System.Web; public class Modify : IHttpHandler
{ private BLL.TransferExecuteAction execute = null;
private Model.TableModel model = null;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
execute = new BLL.TransferExecuteAction(); int id = -;
string cont = string.Empty, msg = string.Empty;
if (!int.TryParse(context.Request.QueryString["id"], out id))//浏览器第二次post请求,表单传值,提交新的数据。
{
id = int.Parse(context.Request.Form["id"]);
if (id > -)
{
model = new Model.TableModel();
model.id = int.Parse(context.Request.Form["id"]);
model.课程编号 = context.Request.Form["num"];
model.课程名称 = context.Request.Form["name"];
model.图书封面 = context.Request.Form["face"];
int gra = ;
int.TryParse(context.Request.Form["grade"], out gra);
model.学分 = (int?)gra;
if ( == execute.ExecuteModifyData(model))
msg = "数据更新成功";
else
msg = "数据更新失败";
}
else
{
msg = "参数错误,数据更新失败";
}
cont = " <script>var time = 4;document.write('" + msg + ",<span id=\"time\" style=\"font-size:25px;color:red;\">3</span> 秒后自动跳转到数据列表页面。');setInterval(function () {if (time > 0) {time--;document.getElementById(\"time\").innerHTML = time;}else {window.location = 'TableList.ashx';}}, 1000);</script>";
}
else//浏览器第一次get请求,url传值,服务器返回真实数据
{
model = execute.ExecuteModel(id);
string num = model.课程编号;
string name = model.课程名称;
string face = model.图书封面;
string grade = ((int?)model.学分).ToString();
cont = System.IO.File.ReadAllText(CommonHelper.GetPhysicsPathOfFile(context, "OperationModel.html"));
cont = cont.Replace("{@Operation}", "更新").Replace("{@action}", "Modify.ashx").Replace("{@id}", id.ToString()).Replace("{@num}", num)
.Replace("{@face}", face).Replace("{@name}", name).Replace("{@grade}", grade.ToString());
}
context.Response.Write(cont);
} public bool IsReusable
{
get
{
return false;
}
} }
OperationModel.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
tr td {
padding: 3px;
margin: 3px auto;
}
</style>
</head>
<body>
<center>
<h3>{@Operation}数据</h3>
<form action="{@action}" method="post">
<table border="0">
<tr>
<td>id</td>
<td><input type="text" name="id" value="{@id}" readonly="readonly"/></td></tr>
<tr>
<td>课程编号</td>
<td><input type="text" name="num" value="{@num}"/></td></tr>
<tr>
<td>图书封面</td>
<td><input type="text" name="face" value="{@face}"/></td></tr>
<tr>
<td>课程名称</td>
<td><input type="text" name="name" value="{@name}"/></td></tr>
<tr>
<td>学分</td>
<td><input type="text" name="grade" value="{@grade}"/></td></tr>
<tr>
<td><input type="submit" name="submit" value="确定" /></td>
<td><input type="button" onclick="window.location = 'TableList.ashx';" value="取消" /></td>
</tr>
</table>
<input type="hidden" name="IsPostBack" value="1" />
</form>
</center>
</body>
</html>
CommonHelper.cs:
public class CommonHelper
{
public CommonHelper()
{
} public static string GetPhysicsPathOfFile(HttpContext context, string fileName)
{
return context.Server.MapPath(fileName);
}
}
Web.config:
<?xml version="1.0"?> <!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration> <system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="sql" connectionString="Data Source=.;Initial CataLog=DBLQBZ;Integrated Security=True"/>
</connectionStrings>
</configuration
三层代码可以参考前几篇的文章。项目文件:http://pan.baidu.com/s/1jG3aS42
ASP.NET的一般处理程序对数据的基本操作的更多相关文章
- Asp.net web服务处理程序(第六篇)
四.Web服务处理程序 对于Web服务来说,标准的方式是使用SOAP协议,在SOAP中,请求和回应的数据通过XML格式进行描述.在Asp.net 4.0下,对于Web服务来说,还可以选择支持Ajax访 ...
- ASP模拟POST请求异步提交数据的方法
这篇文章主要介绍了ASP模拟POST请求异步提交数据的方法,本文使用MSXML2.SERVERXMLHTTP.3.0实现POST请求,需要的朋友可以参考下 有时需要获取远程网站的某些信息,而服务器又限 ...
- Jquery zTree结合Asp.net实现异步加载数据
zTree结合Asp.net实现异步加载数据 实现简单操作 zTree 下载 api 访问 :http://www.ztree.me/v3/main.php 例子中用到json数据转化 newtons ...
- 【第二篇】ASP.NET MVC快速入门之数据注解(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- ASP.NET MVC动态加载数据
ASP.NET MVC动态加载数据,一般的做法是使用$.each方法来循环产生tabel: 你可以在html时先写下非动态的部分: Source Code 上图中,有一行代码: <tbody ...
- Go语言和ASP.NET的一般处理程序在处理WEB请求时的速度比较
Go语言和ASP.NET的一般处理程序在处理WEB请求时的速度比较 1.首先写一个Go语言的简单WEB程序,就返回一个HelloWord! package main import ( f " ...
- 详解ASP.NET提取多层嵌套json数据的方法
本篇文章主要介绍了ASP.NET提取多层嵌套json数据的方法,利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,有兴趣的可以了解一下. 本文实例讲述了ASP.NET利用第三 ...
- ASP.NET Core中返回 json 数据首字母大小写问题
ASP.NET Core中返回 json 数据首字母大小写问题 在asp.net core中使用ajax请求动态绑定数据时遇到该问题 后台返回数据字段首字母为定义的大写,返回的数据没有问题 但是在前台 ...
- MySQL:数据表基本操作
数据表基本操作 注意点: 1.数据表中已经有数据时,轻易修改数据类型,有可能因为不同的数据类型的数据在机器 中存储的方式及长度并不相同,修改数据类型可能会影响到数据表中已有的数据类型. 2. 数据表 ...
随机推荐
- JAVA Callable
Listing -. Calculating Euler’s Number e import java.math.BigDecimal; import java.math.MathContext; i ...
- ServletDemo
1. Servlet 接口 继承 Servlet 接口,实现Servlet 接口的 所有抽象方法! 实现类代码 package xw.servlet; import java.io.IOExcepti ...
- 在Vista或更高版本Windows系统中, 获取超大图标的办法
这几天写个小东西, 需要获取系统正在运行的程序图标, 一般来说32*32就足够了, 不过既然Win7能够支持超大图标(256*256), 咱们也需要与时俱进, 说不定什么时候遇到个变态客户就有这要求了 ...
- I方法 thinkphp
function I($name,$default=null,$filter=null,$datas=null) { static $_PUT = null; $default_filter='htm ...
- zepto-创建dom
在使用zepto时,为了使得ajax请求回来的数据添加到页面上之后,不至于失去所绑定的事件. 我一般会采用$()创建dom元素节点,然后添加所需要的class和html等等一系列的样式,最基本的是$( ...
- android 5.0 (lollipop)源码编译环境搭建(Mac OS X)
硬件环境:MacBook Pro Retina, 13-inch, Late 2013 处理器 2.4 GHz Intel Core i5 内存 8 GB 1600 MHz DDR3 硬盘60G以 ...
- 解决Eclipse启动报错Failed to create the Java Virtual Machine
电脑:2G内存,WIN7 32位. 启动adt-bundle-windows-x86-20140702\eclipse\eclipse.exe时,报错[Failed to create the Jav ...
- xcode5 和code6中push后方法执行的先后问题
在xocde5中 执行的顺序是 prepareForSegue .viewDidLoad. didSelectRowAtIndexPath,在xcode6中 执行的顺序是 prepareForSe ...
- 实验一补充内容 Java开发环境的熟悉-刘蔚然
本次实验 PSP时间统计 步骤 耗时百分比 需求分析 5% 设计 10% 代码实现 67% 测试 15% 分析总结 3%
- 371. Sum of Two Integers
不用加减法计算两个整数的和.这道题其实是考察一些基本的布尔代数知识.我们知道,二进制表示时: 0 + 0 = 00 1 + 0 = 01 0 + 1 = 01 1 + 1 = 10 所以,两个二进制整 ...