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(500);
        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 = 0;
            int.TryParse(context.Request.Form["grade"], out gra);
            model.学分 = (int?)gra;
            execute = new BLL.TransferExecuteAction();
            if (1 == 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 ( 1== 1)
        {
            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 = -1;
        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 > -1)
            {
                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 = 0;
                int.TryParse(context.Request.Form["grade"], out gra);
                model.学分 = (int?)gra;
                if (1 == 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

转载于:https://www.cnblogs.com/wjshan0808/p/3602901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值