这是最近在干一个webform的cms的时候用起来的,原来虽然做过很多技术,什么remoting,wcf,webservice,可是弄来弄去,最后也没个收藏的地儿,全都放在笔记本儿上了,可是人又懒地可以,做来做去,最后它们还是扔在那里
。
说正题吧:最近用到的这套小型的cms,主就是用的链表了,大部分的栏目都在同一张表里,而这张表囊括了几乎所有需要用到的字段,包括图片,内容,一堆乱七八糟的东西,这样做的时候主要就是读这一张表,所以这里做的时候主要就是把配置给抽出来。封装的
作用一直都最关键在委托的使用上,从2.0开始,喜欢用委托,可是手写一个又太麻烦从4.0开发就一直用扩展方法和ACTION FUNC等委托,好 好几年未认真写,如今用来还真的是得心应手。表达思想表达地比较迅速,也容量写好。对于一个对代码有追求的人。应该
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using JGDJWeb.Helper;
using JGDJWeb.Model;
namespace JGDJWeb.Extensions {
public class PageBase : System.Web.UI.Page {
protected PageBase() {
}
protected void InitPage() {
if (!IsPostBack) {
BindModule(this, (a, b) => { BindSingleModules(this, b, a); });
}
}
protected void BindSingleModules(Page page, string suffix, Repeater a) {
using (var context = new JGDJEntities()) {
switch (suffix) {
case "jgxx": //机关快讯
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "机关讯息", context, a, 8, () => { }));
break;
case "tpxw": //机关快讯
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "图片新闻", context, a, 8, () => { }));
break;
case "gwdt": //工委动态
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "工委动态", context, a, 8, () => { }));
break;
case "topywa": //要闻
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "要闻", context, a, 2, () => { }));
break;
case "topywb": //要闻
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "工委动态", context, a, 3, () => { }));
break;
case "gsgg": //公示公告
BindGsggList(context, a);
break;
case "gbrm": //干部任免
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "干部任免", context, a, 8, () => { }));
break;
case "djbd": //党建宝典
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "党建宝典", context, a, 4, () => { }));
break;
case "rdjj": //热点聚焦
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "机关讯息", context, a, 7, () => { }));
break;
case "dyyzh": //党员驿站
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "党员驿站", context, a, 4, () => { }));
break;
case "qtfc": //群团风采
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "群团风采", context, a, 4, () => { }));
break;
case "dflzh": //党风廉政
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "工委动态", context, a, 6, () => { }));
break;
case "sxdzzhjsh": //三党组织建设
BindSingleModuleFromNewsClass(new BindSingleModuleFromNewsClassParams(page, suffix, "工委动态", context, a, 6, () => { }));
break;
case "pic": //焦点图幻灯
BindPics(context, a, 3);
break;
default:
break;
}
}
}
public class BindSingleModuleFromNewsClassParams {
private Page _page;
private string _suffix;
private string _classCName;
private JGDJEntities _context;
private Repeater _a;
private int _takeCount;
private Action _doAlternative;
public BindSingleModuleFromNewsClassParams(Page page, string suffix, string classCName, JGDJEntities context, Repeater a, int takeCount, Action doAlternative) {
_page = page;
_suffix = suffix;
_classCName = classCName;
_context = context;
_a = a;
_takeCount = takeCount;
_doAlternative = doAlternative;
}
public Page Page {
get { return _page; }
}
public string Suffix {
get { return _suffix; }
}
public string ClassCName {
get { return _classCName; }
}
public JGDJEntities Context {
get { return _context; }
}
public Repeater A {
get { return _a; }
}
public int TakeCount {
get { return _takeCount; }
}
public Action DoAlternative {
get { return _doAlternative; }
}
}
protected void BindSingleModuleFromNewsClass(BindSingleModuleFromNewsClassParams bindSingleModuleFromNewsClassParams) {
try {
string jgkxNo = bindSingleModuleFromNewsClassParams.Context.JY_News_Classs.Single(n => n.ClassCName == bindSingleModuleFromNewsClassParams.ClassCName).ClassNo;
jgkxNo = Encrypt.MD5Encrypt(jgkxNo);
List<JY_Newss> newss = Common.GetNewsListByCNo(jgkxNo).OrderByDescending(u => u.CreateTime).ToList();
var news = newss.First();
var newsPicFirst = newss.FirstOrDefault(u => u.PicURL != null);
bindSingleModuleFromNewsClassParams.A.DataSource = newss.Where(n => n.NewsID != news.NewsID).Take(bindSingleModuleFromNewsClassParams.TakeCount);
bindSingleModuleFromNewsClassParams.A.DataBind();
var prefixArray = new String[] { "More_", "Link_", "P_", "A_", "Img_", "H_" };
prefixArray.ToList().ForEach(t => {
switch (t) {
case "More_":
case "Link_":
var anchor = bindSingleModuleFromNewsClassParams.Page.FindControl(t + bindSingleModuleFromNewsClassParams.Suffix) as HtmlAnchor;
if (anchor != null) {
if (anchor.InnerHtml == "") anchor.InnerHtml = news.NewsTitle;
anchor.HRef = "/List.aspx?ClassNo=" +
Encrypt.MD5Encrypt(news.ClassNo);
}
break;
case "A_":
if (newsPicFirst != null) {
var alink = bindSingleModuleFromNewsClassParams.Page.FindControl(t + bindSingleModuleFromNewsClassParams.Suffix) as HtmlAnchor;
if (alink != null) {
if (alink.InnerHtml == "") alink.InnerHtml = news.NewsTitle;
alink.HRef = "/Detail.aspx?ClassNo=" +
Encrypt.MD5Encrypt(newsPicFirst.ClassNo);
}
}
break;
case "P_":
case "H_":
var p =
bindSingleModuleFromNewsClassParams.Page.FindControl(t +
bindSingleModuleFromNewsClassParams
.Suffix) as
HtmlGenericControl;
if (p != null)
p.InnerHtml = news.Contents.NoHtml().ToTitleString(50);
break;
case "Img_":
if (newsPicFirst != null) {
var img =
bindSingleModuleFromNewsClassParams.Page.FindControl(t +
bindSingleModuleFromNewsClassParams
.Suffix) as
HtmlImage;
if (img != null)
img.Src = newsPicFirst.PicURL;
}
break;
case "ImgTitle_":
if (newsPicFirst != null) {
var img =
bindSingleModuleFromNewsClassParams.Page.FindControl(t +
bindSingleModuleFromNewsClassParams
.Suffix) as
HtmlGenericControl;
if (img != null)
img.InnerHtml = newsPicFirst.NewsTitle.ToTitleString(15);
}
break;
default:
break;
}
});
bindSingleModuleFromNewsClassParams.DoAlternative();
} catch (Exception ex) {
}
}
protected void BindGsggList(JGDJEntities context, Repeater a) {
try {
var list =
context.IM_AnnounceNotices.Where(c => c.BEGIN_DATE <= DateTime.Now)
.OrderByDescending(u => u.SEND_TIME)
.Take(5)
.ToList();
a.DataSource = list;
a.DataBind();
} catch (Exception ex) {
}
}
protected static void BindPics(JGDJEntities context, Repeater a, int takenCount) {
try {
//获取所有已经通过审核的新闻,并且新闻属性不能为空,为了方便使用Linq找出具有幻灯属性的新闻,所以取出全部新闻然后过滤
var newsList =
context.JY_Newss.Where(t => t.JY_NewsClass.Any(nc => nc.IsPass == 1) && t.NewsProperty != null).ToList();
//过滤具有幻灯属性的新闻,并取得最新的5条记录
var newsTOP5 =
newsList.Where(c => c.NewsProperty.Split(',')[3] == "1").OrderByDescending(t => t.NewsID).Take(takenCount).ToList();
a.DataSource = newsTOP5;
a.DataBind();
} catch (Exception ex) {
}
}
protected void BindModule(Page page, Action<Repeater, string> bindSingRpt) {
using (var context = new JGDJEntities())
BindControls(page, "List_", bindSingRpt);
}
protected void BindControls(Page page, String prefix, Action<Repeater, string> bindSingControl) {
var controlList = page.Controls;
if (page.Master != null) {
var container = page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
if (container != null)
controlList = container.Controls;
}
foreach (Control control in controlList) {
if (control.ID != null && control.ID.StartsWith(prefix) && control is Repeater) {
var suffix = control.ID.Split('_')[1];
if (prefix == "List_")
bindSingControl(control as Repeater, suffix);
}
}
}
protected void Bind(string className, HtmlAnchor anchor, Repeater repeater, int count) {
try {
using (var context = new JGDJEntities()) {
var classNo = context.JY_News_Classs.Where(n => n.ClassCName == className).First().ClassNo;
var newss = Common.GetNewsListByCNo(Encrypt.MD5Encrypt(classNo));
if (anchor != null) {
anchor.HRef = "/List.aspx?ClassNo=" + Encrypt.MD5Encrypt(classNo);
}
repeater.DataSource = newss.Take(count);
repeater.DataBind();
}
} catch (Exception e) { }
}
#region 网站访问量
protected void pageviews() {
int count = 0;
//数据累加
int Stat = 0;
StreamReader srd;
//取得文件的实际路径
string file_path = Server.MapPath("/XML/counter.txt");
//打开文件进行读取
srd = File.OpenText(file_path);
while (srd.Peek() != -1) {
string str = srd.ReadLine();
count = int.Parse(str);
}
srd.Close();
// 在新会话启动时运行的代码
Application.Lock();
//获取Application对象中保存的网站总访问量
Stat = count;
Stat += 1;
object obj = Stat;
Application["counter"] = obj;
//将数据记录写入文件
StreamWriter srw = new StreamWriter(file_path, false);
srw.WriteLine(Stat);
srw.Close();
Application.UnLock();
}
#endregion
}
}