//UserInfoList.html

<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>用户列表页面</title>
<meta charset="utf-8" />
    <link href="tableStyle.css" rel="stylesheet" />
    <script src="../jquery 3.3.1/jquery-3.3.1.js"></script>
    <script>
        $(function () {
            $('.delCls').click(function () {
                if (!confirm('确定要删除该记录吗?')) {
                    return false;
                }
            });
        });
    </script>
</head>
<body>
    <a href="AddUserInfo.html">添加用户</a>
    <table>
        <tr>
            <th>ID</th>
            <th>名字</th>
            <th>密码</th>
            <th>详情</th>
            <th>删除</th>
            <th>编辑</th>
        </tr>
        $tbody
    </table>
</body>

</html>

//tableStyle.css

caption
        {
            padding: 0 0 5px 0;
            width: 700px;
            font: italic 11px "Trebuchet MS" , Verdana, Arial, Helvetica, sans-serif;
            text-align: right;
        }
        
        th
        {
            font: bold 11px "Trebuchet MS" , Verdana, Arial, Helvetica, sans-serif;
            color: white;
            border-right: 1px solid #C1DAD7;
            border-left: 1px solid #C1DAD7;
            border-bottom: 1px solid #C1DAD7;
            border-top: 1px solid #C1DAD7;
            letter-spacing: 2px;
            text-transform: uppercase;
            text-align: left;
            padding: 6px 6px 6px 12px;
            background: #0066AA no-repeat;
        }
        
        th.nobg
        {
            border-top: 0;
            border-left: 0;
            border-right: 1px solid #C1DAD7;
            background: none;
        }
        
        td
        {
            border-left: 1px solid #C1DAD7;
            border-right: 1px solid #C1DAD7;
            border-bottom: 1px solid #C1DAD7;
            background: #fff;
            font-size: 11px;
            padding: 6px 6px 6px 12px;
            color: #4f6b72;
        }
        
        
        td.alt
        {
            background: #F5FAFA;
            color: #797268;
        }
        
        th.spec
        {
            border-left: 1px solid #C1DAD7;
            border-top: 0;
            background: #fff no-repeat;
            font: bold 10px "Trebuchet MS" , Verdana, Arial, Helvetica, sans-serif;
        }
        
        th.specalt
        {
            border-left: 1px solid #C1DAD7;
            border-top: 0;
            background: #f5fafa no-repeat;
            font: bold 10px "Trebuchet MS" , Verdana, Arial, Helvetica, sans-serif;
            color: #797268;
        }
        
        .span_link
        {
            cursor:pointer;
            color:Black;
        }
        
        .tr_Category
        {
        }
      
        .pageLink
        {
            color:Blue;
             margin-left:2px;
            margin-right:2px;
        }
        
        .tr_Category_P td
        {
           background-color:#EEEEFF;
        }

//UserInfoList.ashx

<%@ WebHandler Language="C#" Class="UserInfoList" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.IO;

public class UserInfoList : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        string filePath = context.Request.MapPath("UserInfoList.html");
        string fileContent = File.ReadAllText(filePath);

        string conString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(conString))
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("select * from UserInfo", conn))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < dt.Rows.Count; i++)
                {                    
                    sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td><a href='UserInfoDetail.ashx?UserId={0}'>详情</a></td><td><a href='DeleteUserInfo.ashx?UserId={0}' class='delCls'>删除</a></td><td><a href='EditUserInfo.ashx?UserId={0}&&UserName={1}&&UserPwd={2}'>编辑</a></td></tr>",
                        dt.Rows[i]["UserId"], dt.Rows[i]["UserName"], dt.Rows[i]["UserPwd"]);

                }
                fileContent = fileContent.Replace("$tbody", sb.ToString());
                context.Response.Write(fileContent);

            }
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

//AddUserInfo.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>添加用户</title>
<meta charset="utf-8" />
</head>
<body>
    <form method="post" action="AddUserInfo.ashx">
        名字<input type="text" name="txtName" value="" />
        密码<input type="text" name="txtPwd" value="" />
        <input type="submit" name="name" value="提交" />
    </form>
</body>

</html>

//AddUserInfo.ashx

<%@ WebHandler Language="C#" Class="AddUserInfo" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class AddUserInfo : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string name = context.Request.Form["txtName"];
        string pwd = context.Request.Form["txtPwd"];
        string conString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(conString))
        {
            string sql = "insert into UserInfo values(@UserName,@UserPwd)";
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                con.Open();
                cmd.Parameters.Add(new SqlParameter("@UserName",name));
                cmd.Parameters.Add(new SqlParameter("@UserPwd",pwd));
                if (cmd.ExecuteNonQuery()>0)
                {
                    context.Response.Redirect("UserInfoList.ashx");
                }
                else
                {
                    context.Response.Redirect("Error.html");
                }
            }
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

//UserInfoDetail.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
<meta charset="utf-8" />
</head>
<body>
    用户名:$name<br />
    密码:$pwd<br />
</body>

</html>

//UserInfoDetail.ashx

<%@ WebHandler Language="C#" Class="UserInfoDetail" %>

using System;
using System.Web;
using System.Text;
using System.IO;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public class UserInfoDetail : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        string filePath = context.Request.MapPath("UserInfoDetail.html");
        string fileContent = File.ReadAllText(filePath);
        int id;
        if (int.TryParse(context.Request.QueryString["UserId"],out id))
        {
            string connString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("select * from UserInfo where UserId=@UserId", conn))
                {
                    adapter.SelectCommand.Parameters.Add(new SqlParameter("@UserId", id));
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    string name = dt.Rows[0]["UserName"].ToString();
                    string pwd = dt.Rows[0]["UserPwd"].ToString();
                    fileContent = fileContent.Replace("$name", name).Replace("$pwd", pwd);
                        context.Response.Write(fileContent);
                }

            }
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

//DeleteUserInfo.ashx

<%@ WebHandler Language="C#" Class="DeleteUserInfo" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class DeleteUserInfo : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        int id = Convert.ToInt32(context.Request.QueryString["UserId"]);
        string connString = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            string sql = "delete from UserInfo where UserId=@UserId";
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                conn.Open();
                cmd.Parameters.Add(new SqlParameter("@UserId", id));
                if (cmd.ExecuteNonQuery()>0)
                {
                    context.Response.Redirect("UserInfoList.ashx");
                }
                else
                {
                    context.Response.Redirect("Error.html");
                }
            }
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

//EditUserInfo.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
<meta charset="utf-8" />
</head>
<body>

    <form action="UpdateUserInfo.ashx" method="post">  
        <input type="hidden" name="txtId" value="$id" />     
        用户名:<input type="text" name="txtName" value="$name" /><br />
        密码:<input type="text" name="txtPwd" value="$pwd" /><br />
        <input type="submit" name="name" value="修改" />
    </form>
</body>

</html>

//EditUserInfo.ashx

<%@ WebHandler Language="C#" Class="EditUserInfo" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class EditUserInfo : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        string id = context.Request.QueryString["UserId"];
        string name = context.Request.QueryString["UserName"];
        string pwd = context.Request.QueryString["UserPwd"];
        string filePath = context.Request.MapPath("EditUserInfo.html");
        string fileContent = System.IO.File.ReadAllText(filePath);
        fileContent = fileContent.Replace("$id", id).Replace("$name", name).Replace("$pwd", pwd);
            context.Response.Write(fileContent);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

//UpdateUserInfo.ashx

<%@ WebHandler Language="C#" Class="UpdateUserInfo" %>

using System;
using System.Web;
    using System.Data;
    using System.Data.SqlClient;

public class UpdateUserInfo : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        int id = Convert.ToInt32(context.Request.Form["txtId"]);
        string newName = context.Request.Form["txtName"];
        string newPwd = context.Request.Form["txtPwd"];
             string connString = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            string sql = "update UserInfo set UserName=@UserName,UserPwd=@UserPwd where UserId=@UserId";
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                conn.Open();
                cmd.Parameters.Add(new SqlParameter("@UserName", newName));
                cmd.Parameters.Add(new SqlParameter("@UserPwd", newPwd));
                cmd.Parameters.Add(new SqlParameter("@UserId", id));
                if (cmd.ExecuteNonQuery()>0)
                {
                    context.Response.Redirect("UserInfoList.ashx");
                }
                else
                {
                    context.Response.Redirect("Error.html");
                }
            }
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

//Error.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
<meta charset="utf-8" />
</head>
<body>
    服务器忙啊啊啊!!
    <span style="font-size:20px;color:red;font-weight:bolder">5</span>秒钟以后自动跳转到<a href="UserInfoList.ashx">列表页面</a>
</body>
</html>

ASP CRUD的更多相关文章

  1. ASP.NET Core Web API Cassandra CRUD 操作

    在本文中,我们将创建一个简单的 Web API 来实现对一个 “todo” 列表的 CRUD 操作,使用 Apache Cassandra 来存储数据,在这里不会创建 UI ,Web API 的测试将 ...

  2. 《Entity Framework 6 Recipes》中文翻译系列 (20) -----第四章 ASP.NET MVC中使用实体框架之在MVC中构建一个CRUD示例

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第四章  ASP.NET MVC中使用实体框架 ASP.NET是一个免费的Web框架 ...

  3. 创建ASP.NET Core MVC应用程序(4)-添加CRUD动作方法和视图

    创建ASP.NET Core MVC应用程序(4)-添加CRUD动作方法和视图 创建CRUD动作方法及视图 参照VS自带的基架(Scaffold)系统-MVC Controller with view ...

  4. 通过Knockout.js + ASP.NET Web API构建一个简单的CRUD应用

    REFERENCE FROM : http://www.cnblogs.com/artech/archive/2012/07/04/Knockout-web-api.html 较之面向最终消费者的网站 ...

  5. CRUD Operations In ASP.NET MVC 5 Using ADO.NET

    Background After awesome response of an published by me in the year 2013: Insert, Update, Delete In ...

  6. 【ASP.NET Web API教程】2.1 创建支持CRUD操作的Web API

    原文 [ASP.NET Web API教程]2.1 创建支持CRUD操作的Web API 2.1 Creating a Web API that Supports CRUD Operations2.1 ...

  7. Bootstrap+Knockout.JS+ASP.Net MVC3+PetaPOCO实现CRUD操作

    Bootstrap+Knockout.JS+ASP.Net MVC3+PetaPOCO实现CRUD操作 1.需求: 1.1)页面要美观大气 1.2)前端代码要简洁清晰,要用MVC或是MVVM框架 1. ...

  8. Asp.Net Web API 2(CRUD操作)第二课

    Asp.Net Web API 2(CRUD操作)第二课 Asp.Net Web API 导航   Asp.Net Web API第一课:入门http://www.cnblogs.com/aehyok ...

  9. ASP.NET Web API 基本操作(CRUD)

    上一篇介绍了ASP.NET Web API的基本知识和原理,这一篇我们通过一个更直观的实例,对产品进行CRUD操作(Create/Read/Update/Delete)来继续了解一下它的基本应用. 创 ...

随机推荐

  1. PHP移动互联网开发笔记(1)——环境搭建及配置

    开篇说明:记得我上大二的时候第一次听到PHP,当时只知道这是一个开发网站的语言,并没有深入学习,在学了Java Web开发和Android开发之后我对互联网的发展方向有了一个我自己的认识,现在我们不能 ...

  2. MySQL 备份错误日志

    MySQL 备份错误日志:   shell> mv host_name.err host_name.err-old shell> mysqladmin -u root -p flush-l ...

  3. Go语言:正則表達式的使用

    Go语言的正則表達式使用非常easy.演示样例代码: package test import ( "fmt" "regexp" ) func RegixBase ...

  4. 基于GTID多源复制扩展

    对一个运行很久的库做备份恢复建同步 不能使用xtrabackup   使用mysqldump导数据: mysqldump -S /data/mysql/3307/tmp/3307.sock --sin ...

  5. SpringMVC大坑一枚:ContentNegotiatingViewResolver可能不利于SEO

    广大站长都有关注自己网站被搜索引擎收录的习惯,最近用百度.360等搜索引擎,查看了自己网站的一些情况,使用命令"site:fansunion.cn". 我发现了一些异常信息,不止一 ...

  6. 配置SVN服务器

    svn启动: 版本控制对于团队合作显得尤为重要,那么如何在iOS开发中进行版本控制呢?在今天的博客中将会介绍如何在MAC下配置SVN服务器,如何导入我们的工程,如何在Xcode中进行工程的checkO ...

  7. 学习鸟哥的Linux私房菜笔记(7)——文件查找与文件管理1

    一.可执行文件的搜索 which 显示一个可执行文件的完整路径 按照alias->$PATH的顺序查找 查看系统的环境变量 whereis 搜索一个可执行工具及其相关配置.帮助 slocate ...

  8. window.url.createobjecturl 兼容多种浏览器(IE,google,360,Safari,firefox)

    <script type="text/javascript"> function setImagePreview() { var docObj = document.g ...

  9. CSS拾遗(一)

    重新看<精通CSS(第二版)>做一些记录,方便今后巩固. 1.外边距叠加 只有普通文档流中块框的垂直外边距才会发生外边距叠加.行内框.浮动框.或绝对定位框之间的外边距不会叠加. 2.相对定 ...

  10. burp suite中国乱码的解决方案

    于http的response还有经常出现乱码,其实解决的办法很easy 首先点击Options标签, 然后找到display 找到http message display watermark/2/te ...