利用拼接字符串的方式来写一个简化版的后台网站(推荐初学者进)
来源:互联网 发布:msqrd相似的软件 编辑:程序博客网 时间:2024/05/16 12:46
今天用一般处理程序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>
其实这种方法大同小异,只要掌握了拼接跳转的技巧,就可以自己写着玩了。
其中涉及了一些数据库的读取,就不详细说了。
0 0
- 利用拼接字符串的方式来写一个简化版的后台网站(推荐初学者进)
- java 利用注释和反射写一个简单的SQL语句拼接程序,很简单初学者
- 分享一个自己写的字符串工具:字符串格式化拼接
- 利用一个辅助变量来实现sql语句的拼接
- iOS拼接字符串的方式
- 拼接字符串的三个方式
- 初学者用thinkphp写的一个有文章分享网站
- 利用OMF来简化数据文件的管理
- OC 简化项目中拼接字符串的方法
- 前台用的是ajax请求,后台json字符串拼接方式
- 分享自己写的一个简化版LOG输出宏
- 字符串拼接的几种方式
- Python字符串拼接的五种方式
- 我是个VB.net的初学者,在这我推荐一个很好的vb.net网站
- 写一个利用ping命令查看网站延迟的方法
- 一个针对初学者的自学顺序推荐
- 推荐一个适合初学者学习编程的网站——codecademy
- 大型网站后台架构的演变(推荐)
- fork/join框架
- 从一个实例中学习DTW算法
- 20150408参赛总结
- 我眼里的正则表达式(入门)
- Makefile文件生成
- 利用拼接字符串的方式来写一个简化版的后台网站(推荐初学者进)
- error LNK1169: 找到一个或多个多重定义的符号D:\Visual Studio 2013\...\Debug\.exe11
- BZOJ1934【最小割】
- Cocos2d-x设计模式 -- 外观模式
- JeeWx 微信开发公开课(Jeewx-API 专题),今晚8点不见不散
- "undefined reference to" 问题解决方法
- 手势识别(一)手势识别器
- xcode 6.1解析外部的ips 崩溃日志
- ExtJS获取父子、兄弟容器元素方法