利用拼接字符串的方式来写一个简化版的后台网站(推荐初学者进)

今天用一般处理程序ashx做一个如下的效果,

技术分享

主要是为了实现功能,界面丑就丑把。

先是显示界面DomeHandler.ashx

   public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";


        StringBuilder sb = new StringBuilder();
        //连接字符串
        string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
        string sql = "select top(10) ProductID, ProductName, PTID, ProductJP, ProductPrice from Products";
        using (SqlConnection conn=new SqlConnection(str))
        {
            using (SqlCommand cmd=new SqlCommand(sql,conn))
            {
                conn.Open();
                using (SqlDataReader reader= cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td><a href='ShowDetail.ashx?id={0}'>详情</a><td/><td><a href='DeleteData.ashx?id={0}' onClick='return confirm(\"是否要删除\")'>删除</a><td/><td><a href='Edit.ashx?id={0}'>修改</a><td/></tr>", reader["ProductID"], reader["ProductName"]);
                    }
                }
            }
        }
        
        //将虚拟路径转化成绝对路径
        string path=context.Request.MapPath("/ListHandler.html");
        string html=System.IO.File.ReadAllText(path);
        html=html.Replace("@changeStr", sb.ToString());

        context.Response.Write(html);
        
    }
 
其中的ListHandler.html是一个模板,也就是一个html页面

用占位符将需要替换的字符放在页面中,再用Replace替换数据即可


然后是详情页面ShowDetail.ashx



public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        //获取传过来的参数
        string id = context.Request.QueryString["id"];
        int sqlId = int.Parse(id);
        
        string str=System.Configuration.ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
        string sql="select ProductID, ProductName, PTID, ProductJP, ProductPrice from Products where ProductID=@id";
        
        //可变字符串
        StringBuilder sb = new StringBuilder();
        
        System.Data.DataTable dt = new System.Data.DataTable();
        using (SqlDataAdapter adapter=new SqlDataAdapter(sql,str))
        {
            adapter.SelectCommand.Parameters.Add("@id", sqlId);
            adapter.Fill(dt);
        }

        sb.AppendFormat("<td>{0}</td>", dt.Rows[0]["ProductID"]);
        sb.AppendFormat("<td>{0}</td>", dt.Rows[0]["ProductName"]);
        sb.AppendFormat("<td>{0}</td>", dt.Rows[0]["ProductJP"]);
        sb.AppendFormat("<td>{0}</td>", dt.Rows[0]["ProductPrice"]);
        
        //获取另一个页面的内容
        string path=context.Request.MapPath("/ShowDataDetail.html");
        string html = File.ReadAllText(path);
        html = html.Replace("@changeStr", sb.ToString());
        context.Response.Write(html);
    }

然后是删除页面DeleteData.ashx


    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        
        //获取ID
        string id = context.Request.QueryString["id"];
        int sqlId;
        int.TryParse(id, out sqlId);
        string sql = "delete from Products where ProductID=@id";
        string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
        if (sqlId==null)
        {
            return;
        }
        using (SqlConnection conn = new SqlConnection(str))
        {
            using (SqlCommand cmd=new SqlCommand(sql,conn))
            {
                cmd.Parameters.Add("@id", sqlId);
                conn.Open();
                int rows = cmd.ExecuteNonQuery();
                if (rows > 0)
                {
                    context.Response.Redirect("DomeHandler.ashx");
                }
                else
                {
                    context.Response.Write("删除失败");
                }
            }
        }
            
    }

最后稍难的修改页面Edit.ashx


   public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        
        //获取是展示还是提交
        if (context.Request["edit"] == "edit")
        {
            string id = context.Request["id"];
            string productName = context.Request["ProductName"];
            string productPrice = context.Request["ProductPrice"];
            int sqlId;
            int.TryParse(id, out sqlId);
            if (sqlId == null)
            {
                return;
            }

            string sql = "update Products set ProductName=@ProductName,ProductPrice=@ProductPrice where ProductID=@id";
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(str))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.Add("@id", sqlId);
                    cmd.Parameters.Add("@ProductName", productName);
                    cmd.Parameters.Add("@ProductPrice", productPrice);
                    conn.Open();
                    int rows = cmd.ExecuteNonQuery();
                    if (rows > 0)
                    {
                        //跳转页面到展示界面
                        context.Response.Redirect("DomeHandler.ashx");
                    }
                    else
                    {
                        context.Response.Write("修改失败");
                    }
                }
            }
        }
        else
        {
            //获取ID
            string id = context.Request.QueryString["id"];
            int sqlId;
            int.TryParse(id, out sqlId);
            string sql = "select ProductID, ProductName, ProductPrice from Products where ProductID=@id";
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
            System.Data.DataTable dt = new System.Data.DataTable();
            
            if (sqlId == null)
            {
                return;
            }
            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, str))
            {
                adapter.SelectCommand.Parameters.Add("@id", sqlId);
                adapter.Fill(dt);
            }

            string html=System.IO.File.ReadAllText(context.Request.MapPath("Edit.html"));
            html = html.Replace("@id", dt.Rows[0]["ProductID"].ToString());
            html = html.Replace("@ProductName", dt.Rows[0]["ProductName"].ToString());
            html = html.Replace("@ProductPrice", dt.Rows[0]["ProductPrice"].ToString());

            context.Response.Write(html);
        }
    }
 

再发一下Edit.html


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>

   //一提交就交付给Edit.ashx?edit=edit来处理
    <form method="post" action="Edit.ashx?edit=edit">
    //隐藏字段,只用来自己用的
    <input type="hidden" name="id" value="@id" />
        <table>
            <tr>
                <td>ProductName:
                <input type="text" name="ProductName" value="@ProductName" />
                </td>
            </tr>
            <tr>
                <td>ProductPrice:
                <input type="text" name="ProductPrice" value="@ProductPrice" />
                </td>
            </tr>
            <tr>
                <td>ProductName:
                <input type="submit" name="sub" value="提交" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

其实这种方法大同小异,只要掌握了拼接跳转的技巧,就可以自己写着玩了。

其中涉及了一些数据库的读取,就不详细说了。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。