博客
关于我
MasterPage(母板页)的不一般用法
阅读量:801 次
发布时间:2023-02-07

本文共 1315 字,大约阅读时间需要 4 分钟。

在ASP.NET项目中,通常我们会在页面前面添加编译指令来指定母板页。然而,通过page对象的MasterPageFile属性动态指定母板页也是一个可行的方法。这种方式通常在PreInit事件中进行配置。例如,可以在BasePage类的构造函数中添加事件处理程序:

public class BasePage : Page{    public BasePage()    {        PreInit += Set_MasterPage;    }    private void Set_MasterPage(object sender, EventArgs e)    {        this.MasterPageFile = Request.Path + @"../jj.Master";    }}

继承自BasePage类的页面将使用jj.Master作为母板页。这种方式的优势在于,只需改变一处代码即可更换母板页,每个母板页对应一个继承自Page类的自定义页面类。

在母板页的代码文件中,可以定义各种字段和属性供前端模板使用。这种方式可以实现链接和路径的分离,便于开发和部署。即使大段内容也可以分离,例如:

protected TemplateInfo TemplateInfo{    get { return TemplateInfo.Instance; }}

通过Singleton模式,可以将模板信息存储在单例中:

public class TemplateInfo{    private static readonly TemplateInfo Instance = new TemplateInfo();    public static TemplateInfo Instance    {        get { return Instance; }    }    private static DateTime scriptTempLastTime;    private static string scriptHtml;    public static string ScriptHtml    {        get        {            string path = "";            DateTime lastDT = File.GetLastWriteTime(path);            if (scriptTempLastTime < lastDT || string.IsNullOrEmpty(scriptHtml))            {                scriptTempLastTime = lastDT;                scriptHtml = File.ReadAllText(path);            }            return scriptHtml;        }    }}

这种方法的优势是,母板页的变化量可以分离到外部文件中,便于维护和部署。

转载地址:http://hzyfk.baihongyu.com/

你可能感兴趣的文章
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
ORM sqlachemy学习
查看>>
Ormlite数据库
查看>>
orm总结
查看>>
os.environ 没有设置环境变量
查看>>
os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
查看>>
os.removexattr 的 Python 文档——‘*‘(星号)参数是什么意思?
查看>>
os.system 在 Python 中不起作用
查看>>
OS2ATC2017:阿里研究员林昊畅谈操作系统创新与挑战
查看>>
OSCACHE介绍
查看>>
SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
查看>>
OSChina 周五乱弹 ——吹牛扯淡的耽误你们学习进步了
查看>>
SQL--mysql索引
查看>>
OSChina 周四乱弹 ——程序员为啥要买苹果手机啊?
查看>>
OSChina 周日乱弹 —— 2014 年各种奇葩评论集合
查看>>
OSChina 技术周刊第十期,每周技术抢先看!
查看>>
OSError: no library called “cairo-2“ was foundno library called “cairo“ was foundno library called
查看>>
OSError: [WinError 193] %1 不是有效的 Win32 应用程序。
查看>>
osgearth介绍
查看>>
OSGi与Maven、Eclipse PlugIn的区别
查看>>