BS(三层)增删改查——一般处理程序(ashx)版本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fjxcsdn/article/details/85913416

    今天我们学习一下ASP.Net 的增删改查,这个和以往的CS增删改查最大的区别就是U层。以往我们学CS的时候都是用的winform窗体,我们可以直接在窗体中添加控件。然后针对控件的事件属性进行一系列的操作。到了BS,U层变成了空白的浏览器界面,不能直接的拖拉控件,一切都很随意,如果对前端知识了解少的话,真的是很难下手。

  在稍微了解BS之后,我们先来看一篇短短的博客web网站与Web应用程序

  以下的增删改查我们用的是一般处理程序(ashx)

查:(select)

   第一步: 一个HTML的静态页面设计浏览器的框架,好比一个winform窗体,里面上设计好我们需要的控件。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="JS/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $(".deletes").click(function () {
                if (!confirm("确定要删除么")) {
                    return false;
                }
            });
        });
    </script>
</head>
<body>
    <a href="AddUserInfo.html">添加</a>
    <table border="1">
        <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>邮箱</th>
            <th>时间</th>
            <th>删除</th>
            <th>详细</th>
            <th>编辑</th>
        </tr>
        @tbody
    </table>
</body>
</html>

第二步:一个加载用户信息的动态页面,也就是C#代码界面

 public class HanderUserInfo : IHttpHandler
    {
        /// <summary>
        /// 获取数据库中所有用户的信息
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {  //输出内容类型为Html
            context.Response.ContentType = "text/html";
            userInfoBLL bll = new userInfoBLL();
            //接收返回来的数据
            List<userInfo> list = bll.GetList();
            StringBuilder sb = new StringBuilder();
            //遍历实体信息
            foreach (userInfo user in list)
            {
                sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td><a href='DeleteUser.ashx?id={0}'class='deletes'>删除</a></td><td><a href='ShowDetial.ashx?uid={0}'>详细</a></td><td><a href='EditUserInfo.ashx?id={0}'>编辑</a></td></tr>", user.Id,user.userName,user.userPass,user.Email,user.regTime);
            }
            //获取模板路径
            string filepath = context.Request.MapPath("userInfoHtml.html");
            //读取模板文件内容,并给其赋值
            string fileContent = File.ReadAllText(filepath);
            fileContent = fileContent.Replace("@tbody", sb.ToString());
            //输出给浏览器
            context.Response.Write(fileContent);           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

获取部分用户信息

第一步:一个单个用户信息的Html界面

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <table border="1">
        <tr>
            <td>用户名</td>
            <td>$name</td>
        </tr>
        <tr>
            <td>密码</td>
            <td>$pwd</td>
        </tr>
    </table>
</body>
</html>

第二步:从数据库中获取用户信息,加载到HTML界面

public class ShowDetial : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            int id;
            if (int.TryParse(context.Request.QueryString["uid"], out id))
            {
                userInfoBLL Bll = new userInfoBLL();
                userInfo user = Bll.GetUserInfo(id);
                if (user!=null)
                {                   
                    //获取模板路径
                    string filepath = context.Request.MapPath("ShowDetial.html");
                    //读取模板文件内容,并给其赋值
                    string fileContent = File.ReadAllText(filepath);
                    fileContent = fileContent.Replace("$name", user.userName).Replace("$pwd",user.userPass);
                    //输出给浏览器
                    context.Response.Write(fileContent);
                }
                else
                {
                    context.Response.Redirect("Error.html");
                }
            }
            else
            {
                context.Response.Write("参数错误");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

增 (添加用户信息)(insert)

  第一步新增一个静态的添加用户的Html界面

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>添加用户</title>
</head>
<body>
    <!--表单就是用来收集用户数据的,method就是表单的提交方式,action为所指的动态页面-->
    <form method="post" action="AddUserInfo.ashx">
        用户名:<input type="text" name="txtName"><br />
        密码:<input type="password" name="txtPwd"><br />
        邮箱:<input type="text" name="txtMail"><br />
        <input type="submit" value="添加" />
    </form>
</body>
</html>

第二步:提交用户信息

 public class AddUserInfo : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //接收用户输入的数据
            string username = context.Request.Form["txtName"];
            string userPwd = context.Request.Form["txtPwd"];
            string userMail = context.Request.Form["txtMail"];
            //将数据添加到实体
            userInfo user = new userInfo()
            {
                userName = username,
                userPass = userPwd,
                Email = userMail,
                regTime = DateTime.Now,
            };
            userInfoBLL bll = new userInfoBLL();
            if (bll.AddUserInfo(user))
            {   //添加成功后返回到handerUserinfo.ashx界面,重新加载数据库数据
                context.Response.Redirect("HanderUserInfo.ashx");
            }
            else
            {   //跳转到出错界面
                context.Response.Redirect("Error.html");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 删(delete)直接在加载用户界面进行删除

  从数据库中直接删除用户

public class DeleteUser : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int id;            
            if(int.TryParse(context.Request.QueryString["id"],out id))
            {
                userInfoBLL Bll = new userInfoBLL();
                if(Bll.DeleteUserInfo(id))
                {
                    //删除成功后返回到handerUserinfo.ashx界面,重新数据库数据
                    context.Response.Redirect("HanderUserInfo.ashx");
                }
                else
                {                
                    context.Response.Redirect("Error.html");
                }
            }
            else
            {
                context.Response.Write("参数错误");
            }
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

改:(update)

一个修改的静态Html页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <form method="post" action="Edituser.ashx">
        用户名:<input type="text" name="txtName" value="$name"><br />
        密  码:<input type="password" name="txtPwd" value="$pwd"><br />
        邮  箱:<input type="password" name="txtMail"value="$mail"><br />
        <input type="hidden" name="txtID" value="$Id" />
        <input type="submit" value="修改用户" />
    </form>
</body>
</html>

将获取的单个用户信息加载到静态Html界面中

public class ShowEdit : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            int id;
            if (int.TryParse(context.Request.QueryString["id"], out id))
            {
                userInfoBLL Bll = new userInfoBLL();
                userInfo user = Bll.GetUserInfo(id);
                if (user != null)
                {
                    //获取模板路径
                    string filepath = context.Request.MapPath("EditUser.html");
                    //读取模板文件内容,并给其赋值
                    string fileContent = File.ReadAllText(filepath);
                    fileContent = fileContent.Replace("$name", user.userName).Replace("$pwd", user.userPass).Replace("$mail", user.Email).Replace("$Id", user.Id.ToString());
                    //输出给浏览器
                    context.Response.Write(fileContent);
                }
                else
                {
                    context.Response.Write("不存在该人信息");
                }
            }
            else
            {
                context.Response.Write("参数错误");
            }        
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

修改用户信息,提交给数据库

public class Edituser : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int id = Convert.ToInt32(context.Request.Form["txtId"]);
            userInfoBLL bll = new userInfoBLL();
            userInfo user = bll.GetUserInfo(id);
            user.userName = context.Request.Form["txtName"];
            user.userPass= context.Request.Form["txtPwd"];
            user.Email = context.Request.Form["txtMail"];
            if(bll.UpdateUser(user))
            {
                context.Response.Redirect("HanderUserInfo.ashx");
            }
            else
            {
                context.Response.Redirect("Error.html");
            }        
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

扩展:一个出错的HTML 界面

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>   
    <script type="text/javascript">
      window.onload =function() {
          setTimeout(change,1000);
        }
        function change() {
            var time = document.getElementById("time").innerHTML;
            time = parseInt(time);
            time--;
            if (time < 1) {
                window.Location.href = "HanderUserInfo.ashx"
            }
            else {
                document.getElementById("time").innerHTML = time;
                setTimeout(change, 1000)
            }
        }     
    </script>
</head>
<body>
    服务器忙!<span style="font-size:20px;color:red "id="time">5</span>秒钟后自动跳转<a href="HanderUserInfo.ashx">用户列表</a>
</body>
</html>

总结

Winform窗体好比一个个的静态的Html界面

winform窗体后面的代码,无论是获取用户信息,还是对用户信息的操作都是一个个的动态界面,也就是所谓的C#代码界面。

效果展示:

今天的分享就先到这里,下篇博客我们将用aspx写一遍增删改查!

猜你喜欢

转载自blog.csdn.net/fjxcsdn/article/details/85913416