
MasterPage(母板页)的不一般用法
发布日期:2025-04-12 02:37:24
浏览次数:9
分类:精选文章
本文共 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; } }}
这种方法的优势是,母板页的变化量可以分离到外部文件中,便于维护和部署。
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年05月22日 18时19分34秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MapReduce分布编程模型之函数式编程范式
2025-04-11
MapReduce实现二度好友关系
2025-04-11
MapReduce的思想
2025-04-11
MapReduce的模式、算法和用例
2025-04-11
MapReduce的编程思想(1)
2025-04-12
MapReduce程序依赖的jar包
2025-04-12
MapReduce程序(一)——wordCount
2025-04-12
MapReduce编程模型简介和总结
2025-04-12
MapReduce:大数据处理的范式
2025-04-12
MapStruct 对象间属性复制
2025-04-12
MapStruct 映射过程中忽略某个字段
2025-04-12
MapStruct 超神进阶用法,让你的代码效率提升十倍!
2025-04-12
MapStruct使用工具类中的方法来映射字段
2025-04-12
MapStruct的使用教程
2025-04-12
MapXtreme 2005 学习心得 一些基础函数代码(四)
2025-04-12
MapXtreme 2005 学习心得 画道路区域(十二)
2025-04-12
Map中key和value值是否可以为null或空字符串?
2025-04-12
Map俩种遍历方式
2025-04-12
map函数
2025-04-12
map反转key value
2025-04-12