zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    zabbix监控部署(三)
    zabbix监控部署(二)
    zabbix监控部署(一)
    Docker安装mysql5.7
    Docker修改容器默认存储路径
    MySQL全量+增量备份脚本
    企业级Dokcer镜像仓库Harbor部署
    Linux环境pyhon3安装pyinstaller
    配置Hive元数据数据库为PostgreSQL
    Oracle 11gR2 RAC 添加节点
  • 原文地址:https://www.cnblogs.com/wjshan0808/p/3602901.html
Copyright © 2011-2022 走看看