利用拼接字符串的方式来写一个简化版的后台网站(推荐初学者进)
2015-04-09 11:16
323 查看
今天用一般处理程序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)
{
//跳转页面到展示界面
4000
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>
其实这种方法大同小异,只要掌握了拼接跳转的技巧,就可以自己写着玩了。
其中涉及了一些数据库的读取,就不详细说了。
主要是为了实现功能,界面丑就丑把。
先是显示界面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)
{
//跳转页面到展示界面
4000
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>
其实这种方法大同小异,只要掌握了拼接跳转的技巧,就可以自己写着玩了。
其中涉及了一些数据库的读取,就不详细说了。
相关文章推荐
- 最近在一个外网的网站,困扰了我多天的循环和大家分享下,也做为我以后工作之用.第一种(信息作用循环.实现方式后台时钟在前台循环滚动(左右)显示).
- 畅想听吧 cxt8.com 系统bug,利用抓包拼接方式获取网站资源
- 将页面多个下拉框的值以字符串拼接方式存放至数据库一个字段中
- 推荐一个适合初学者学习编程的网站——codecademy
- 推荐一个比较不错JSON字符串处理网站,虽然性能不是很强,但是还不错比起其他的
- 推荐一个非常棒的连接字符串大全的国外网站!
- 某听书网站系统漏洞,利用抓包拼接方式获取网站资源
- 如何将Oracle中同一列的多行记录拼接成一个字符串 十一__262 | 浏览 8183 次 推荐于2016-01-25 17:34:18 最佳答案 需要用wm_concat函数来实现。
- Golang 如何定义一个接口类型的切片,它可以用来存储混合类型的数据,又如何自定义错误信息输出,以及如何定义变参函数,还有字符串多种拼接方式
- 推荐一个非常棒的连接字符串大全的国外网站!
- 推荐一个比较不错JSON字符串处理网站,虽然性能不是很强,但是还不错比起其他的
- java 利用注释和反射写一个简单的SQL语句拼接程序,很简单初学者
- 前台用的是ajax请求,后台json字符串拼接方式
- 推荐一个网站--------皮皮书屋,虽然没用过,但是网站注册的方式会让程序员们很欣喜若狂的~~
- 我是个VB.net的初学者,在这我推荐一个很好的vb.net网站
- 推荐一个WINDOWS系统文件介绍的网站
- 利用WSS做后台存储--设计一个统一的信息发布平台
- 推荐一个读书的网站
- 推荐一个功能强大的在线翻译网站
- 推荐一个网站,太强了。。