设计思想:
使用用户控件(ascx)作为模版,模版中以PlaceHolder作为填充标签,同时兼容诸如风迅等CMS系统中类似的Tag(标签){tags}替换,还支持嵌入用户控件等.这样做不但具有很强的扩展性,还具备很强的操作性.现公布核心代码,供大家参考:
有问题可以随时留言评论,后期我会公布更多关于CMS相关的核心代码:
///
/// 导入文件内容
///
private void LoadContent()
{
string appRoot = UrlHelper.GetApplicationPath();
///判断模版是否存在
if (this._template != null)
{
this._templateControl = (BaseTemplate)this.LoadControl(appRoot + this._template.Path);///加载模版
// Register stylesheet that belongs to the template.
RegisterStylesheet("maincss", appRoot + this._template.BasePath + "/Css/" + this._template.Css);
}
else
{
this._templateControl = (BaseTemplate)this.LoadControl(appRoot + "Templates/DivBlue/Article.ascx");///加载默认模版
// Register stylesheet that belongs to the template.
RegisterStylesheet("maincss", appRoot + "Templates/DivBlue/Css/men.css" );///加载默认的样式
}
///
this._templateControl.ID = "p";
///设置标题
if (!string.IsNullOrEmpty(this._pgObject.Title))
{
this._templateControl.Title = this._pgObject.Title;
}
else
{
this._templateControl.Title = this._currentSite.Name ;
}
//Register the metatags
if (!string.IsNullOrEmpty(this._pgObject.MataKeywors))
{
RegisterMetaTag("keywords", this._pgObject.MataKeywors);
}
else
{
RegisterMetaTag("keywords", this._currentSite.MetaKeywords);
}
if (!string.IsNullOrEmpty(this._pgObject.MetaDescription))
{
RegisterMetaTag("description", this._pgObject.MetaDescription);
}
else
{
RegisterMetaTag("description", this._currentSite.MetaDescription);
}
RegisterMetaTag("Robots", "all");
RegisterMetaTag("Author", "武汉泛新科技有限公司 http://www.027fs.com ,(数字会展)http://www.hbdes.cn ,(流量分析)http://wtas.027fs.com,(强大的内容管理系统)http://fscms.027fs.com");
RegisterMetaTag("Generator", "FSCMS1.0");
///根据实体对象反射填充PlaceHoder内容
///
if (this._entity != null)
{
foreach (DictionaryEntry plh in this._templateControl.Containers)
{
string plhId = plh.Key.ToString();
PlaceHolder placeHodler = (PlaceHolder)plh.Value;
LiteralControl htmlControl = new LiteralControl();
htmlControl.ID = "tx" + plhId;
placeHodler.Controls.Add(htmlControl);
ProjectBase.Data.ObjectUtils.BindObjectToControls(this._entity, placeHodler);
if (string.IsNullOrEmpty(htmlControl.Text))
placeHodler.Controls.Remove(htmlControl);
// placeHodler.Controls.Add(LoadContentFromEntity(plhId, this._entity));
//string placeHodlerContent = LoadContentFromEntity(placeHodler, this._entity); ///从实体对象寻找数据填充该PlaceHoder
//if (placeHodlerContent != null)
//{
// HtmlControl html = new LiteralControl(placeHodlerContent);
// placeHodler.Controls.Add(html);
//}
}
}
////填充个性化标签内容
if(this._particularTags!=null)
{
foreach (DictionaryEntry pTag in this._particularTags)
{
string tagId = pTag.Key.ToString();
Control control= this._templateControl.FindControl(tagId);
if (control != null)
{
PlaceHolder pPlaceHodler = (PlaceHolder)control;
LiteralControl pHtml = new LiteralControl(pTag.Value.ToString());
pPlaceHodler.Controls.Add(pHtml);
}
}
}
///区域控件全拷贝
if (this._placeHolderTags != null)
{
foreach (DictionaryEntry phTag in this._placeHolderTags)
{
string phTagId = phTag.Key.ToString();
Control control = this._templateControl.FindControl(phTagId);
if (control != null)
{
PlaceHolder pPlaceHodler = (PlaceHolder)control;
pPlaceHodler.Controls.Add((PlaceHolder)phTag.Value);
//control = (PlaceHolder)phTag.Value; ///将设置的区域对象直接复制拷贝原对象
}
}
}
this.Controls.AddAt(0, this._templateControl);
// remove html that was in the original page (Default.aspx) ///清除其它内容
for (int i = this.Controls.Count - 1; i > 0; i--)
this.Controls.RemoveAt(i);
}
本文作者:威我泛新 来源:http://kongjian.baidu.com/
CIO之家 www.ciozj.com 微信公众号:imciow