到很多.net的程序实现模板都是使用标签替换,和ASP PHP等没有区别,如dxbbs等.
但是,大家都知道,在.net中,一个页面是由几个部分类组成:xxx.aspx.cs,xxx.cs,xxx.desinger.cs.
那么,我们就可以把一个aspx文件当着模板,使用urlrewriter与反射来实现模板更换.如:一个列表页里面规定必须有一个id叫做rpt的repeater控件,模板在制作时就加上这个控件.而内容页的话,可以使用反射,把数据库实体对象直接绑定到页面控件,方法如下:
///
/// 将控件绑定到对象
///
/// 对象
/// 控件容器
public static void BindControlsToObject(object obj, Control container)
{
if (obj != null)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (PropertyInfo info in properties)
{
try
{
Control control = container.FindControl(info.Name);
if (control != null)
{
if (control is ListControl)
{
ListControl control2 = (ListControl)control;
if (control2.SelectedItem != null)
{
info.SetValue(obj, Convert.ChangeType(control2.SelectedItem.Value, info.PropertyType), null);
}
}
else if (control is CheckBox)
{
CheckBox box = (CheckBox)control;
info.SetValue(obj, Convert.ChangeType(box.Checked, info.PropertyType), null);
}
else
{
PropertyInfo[] controlPropertiesArray = control.GetType().GetProperties();
bool flag = false;
flag = FindAndGetControlProperty(obj, info, control, controlPropertiesArray, "Checked", typeof(bool));
if (!flag)
{
flag = FindAndGetControlProperty(obj, info, control, controlPropertiesArray, "Text", typeof(string));
}
if (!flag)
{
flag = FindAndGetControlProperty(obj, info, control, controlPropertiesArray, "Value", typeof(string));
}
if (!flag)
{
flag = FindAndGetControlProperty(obj, info, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));
}
}
}
}
catch
{
}
}
}
}
同时,使用URLREWRITER,不同的模板时指向不同的ASPX页面,ASPX页面的PAGE节的Inherits当然要写好.有点像微软的MVC的那种模式.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-536618/
,如需转载,请注明出处,否则将追究法律责任。