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>&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;</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>&nbsp;秒后自动跳转到数据列表页面。');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>&nbsp;秒后自动跳转到数据列表页面。');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>&nbsp;秒后自动跳转到数据列表页面。');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的一般处理程序对数据的基本操作的更多相关文章

  1. Asp.net web服务处理程序(第六篇)

    四.Web服务处理程序 对于Web服务来说,标准的方式是使用SOAP协议,在SOAP中,请求和回应的数据通过XML格式进行描述.在Asp.net 4.0下,对于Web服务来说,还可以选择支持Ajax访 ...

  2. ASP模拟POST请求异步提交数据的方法

    这篇文章主要介绍了ASP模拟POST请求异步提交数据的方法,本文使用MSXML2.SERVERXMLHTTP.3.0实现POST请求,需要的朋友可以参考下 有时需要获取远程网站的某些信息,而服务器又限 ...

  3. Jquery zTree结合Asp.net实现异步加载数据

    zTree结合Asp.net实现异步加载数据 实现简单操作 zTree 下载 api 访问 :http://www.ztree.me/v3/main.php 例子中用到json数据转化 newtons ...

  4. 【第二篇】ASP.NET MVC快速入门之数据注解(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  5. ASP.NET MVC动态加载数据

    ASP.NET MVC动态加载数据,一般的做法是使用$.each方法来循环产生tabel: 你可以在html时先写下非动态的部分:  Source Code 上图中,有一行代码: <tbody ...

  6. Go语言和ASP.NET的一般处理程序在处理WEB请求时的速度比较

    Go语言和ASP.NET的一般处理程序在处理WEB请求时的速度比较 1.首先写一个Go语言的简单WEB程序,就返回一个HelloWord! package main import ( f " ...

  7. 详解ASP.NET提取多层嵌套json数据的方法

    本篇文章主要介绍了ASP.NET提取多层嵌套json数据的方法,利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,有兴趣的可以了解一下. 本文实例讲述了ASP.NET利用第三 ...

  8. ASP.NET Core中返回 json 数据首字母大小写问题

    ASP.NET Core中返回 json 数据首字母大小写问题 在asp.net core中使用ajax请求动态绑定数据时遇到该问题 后台返回数据字段首字母为定义的大写,返回的数据没有问题 但是在前台 ...

  9. MySQL:数据表基本操作

    数据表基本操作 注意点: 1.数据表中已经有数据时,轻易修改数据类型,有可能因为不同的数据类型的数据在机器 中存储的方式及长度并不相同,修改数据类型可能会影响到数据表中已有的数据类型. 2.  数据表 ...

随机推荐

  1. Apache Camel

    Apache Camel 1 import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; i ...

  2. SQL实现将一个表的数据插入到另外一个表的代码

    --第一种情况的 1>如果2张表的字段一致,并且希望插入全部数据,可以用这种方法: INSERT INTO 目标表 SELECT * FROM 来源表; 2>比如要将 articles 表 ...

  3. C# .Net实现URL绝对路径和相对路径之间互相转换

    网站制作开发中,URL的绝对路径和相对路径之间互相转换,是经常需要用到的.以下是在C#.Net下一种实现二者互相转化的方法: [DllImport("shlwapi.dll", C ...

  4. jeecg的cq查询方式

    @RequestMapping(params = "datagrid")    public void datagrid(TbStudentDepEntity tbStudentD ...

  5. docker sonaqube

    postgresql:   image: orchardup/postgresql:latest   environment:   - POSTGRESQL_USER=sonar   - POSTGR ...

  6. Android高级之第十一讲Hybird开发

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 随着移动端应用平台的兴起,需求和交互方式的多样化,H5开发逐渐在移动端流行起来:常见的移动产品有We ...

  7. Unix网络编程(迭代服务器)

    #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/soc ...

  8. ActiveMQ持久化消息

    ActiveMQ的另一个问题就是只要是软件就有可能挂掉,挂掉不可怕,怕的是挂掉之后把信息给丢了,所以本节分析一下几种持久化方式: 一.持久化为文件 ActiveMQ默认就支持这种方式,只要在发消息时设 ...

  9. iOS 调出storyboard里面起始Controller的箭头

    在storyboard里面,如果第一个ViewController不是默认的ViewController的时候,我们就需要拖拽一个出来. 如果把默认的ViewController删掉的话,前面的箭头, ...

  10. 测试环境下将centos6.8升级到centos7的操作记录(转)

    在测试环境下安装openstack,由于在centos6下安装openstack,针对源的问题有很多,安装起来很不顺利! 但是在centos7下安装却很顺利,所以考虑将服务器由centos6升级到ce ...