使用一般处理程序实现数据库中新闻信息的增删改查(基于三层架构):
NewsListTemp.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>
<script src="../Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function() {
//给所有的删除超级链接绑定点击的事件
$("a:contains('删除')").click(function() {
return confirm("是否真的的删除?");
});
});
</script>
</head>
<body>
<table>
<tr>
<th>编号</th><th>标题</th><th>日期</th><th>创建人</th><th>操作</th>
</tr>
@trbody
</table>
</body>
</html>
一般处理程序NewsList.ashx
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using WebDemo.BLL;
namespace WebDemo.Web.News
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class NewsList : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//拿到所有的新闻
BLL.HKSJ_Main mainService =new HKSJ_Main();
List<Model.HKSJ_Main> mainList= mainService.GetModelList(string.Empty);
StringBuilder sb =new StringBuilder();
foreach (var hksjMain in mainList)
{
//<th>编号</th><th>标题</th><th>日期</th><th>创建人</th><th>操作</th>
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td><a href='DeleteNews.ashx?id={0}'>删除</a> <a href='EditNews.ashx?id={0}'>修改</a></td></tr>",
hksjMain.ID,
hksjMain.title,
hksjMain.Date,
hksjMain.people
);
}
string tempString = File.ReadAllText(context.Request.MapPath("NewsListTemp.html"));
context.Response.Write(tempString.Replace("@trbody", sb.ToString()));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
编辑页面:EditNewsTemp.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>
<form method="POST" action="ProcessEditNews.ashx">
<table>
<tr>
<td>新闻编号:</td>
<td>@ID
<input type="hidden" name="hidId" value="@ID"/>
</td>
</tr>
<tr>
<td>新闻标题:</td>
<td><input type="text" name="title" value="@title" /></td>
</tr>
<tr>
<td>新闻发布人:</td>
<td><input type="text" name="people" value="@people" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="修改" />
</td>
</tr>
</table>
</form>
</body>
</html>
编辑页面的处理逻辑:EditNews.ashx
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using WebDemo.BLL;
namespace WebDemo.Web.News
{
/// <summary>
/// EditNews 的摘要说明
/// </summary>
public class EditNews : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string stringTemp = File.ReadAllText(context.Request.MapPath("EditNewsTemp.html"));
int id = int.Parse(context.Request["id"] ?? "0");
BLL.HKSJ_Main mainService =new HKSJ_Main();
Model.HKSJ_Main mainModel = mainService.GetModel(id);
stringTemp =
stringTemp.Replace("@ID", mainModel.ID.ToString())
.Replace("@title", mainModel.title)
.Replace("@people", mainModel.people);
context.Response.Write(stringTemp);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
ProcessEditNews.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebDemo.BLL;
namespace WebDemo.Web.News
{
/// <summary>
/// ProcessEditNews 的摘要说明
/// </summary>
public class ProcessEditNews : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int id = int.Parse(context.Request["hidId"] ?? "0");
BLL.HKSJ_Main mainService =new HKSJ_Main();
var news = mainService.GetModel(id);
news.title = context.Request["title"];
news.people = context.Request["people"];
mainService.Update(news);
context.Response.Redirect("NewsList.ashx");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
删除页面的处理逻辑:DeleteNews.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebDemo.BLL;
namespace WebDemo.Web.News
{
/// <summary>
/// DeleteNews 的摘要说明
/// </summary>
public class DeleteNews : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int id = int.Parse(context.Request["id"]??"0");
BLL.HKSJ_Main mainService =new HKSJ_Main();
mainService.Delete(id);
//页面跳转转回首页
context.Response.Redirect("NewsList.ashx");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
说点什么
欢迎讨论