惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
T
Tenable Blog
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Security Latest
Security Latest
P
Privacy & Cybersecurity Law Blog
Google Online Security Blog
Google Online Security Blog
SecWiki News
SecWiki News
P
Palo Alto Networks Blog
TaoSecurity Blog
TaoSecurity Blog
Webroot Blog
Webroot Blog
Spread Privacy
Spread Privacy
O
OpenAI News
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed
C
Check Point Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
人人都是产品经理
人人都是产品经理
S
Security @ Cisco Blogs
Scott Helme
Scott Helme
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
T
Troy Hunt's Blog
W
WeLiveSecurity
GbyAI
GbyAI
N
News | PayPal Newsroom
Y
Y Combinator Blog
C
Cisco Blogs
H
Help Net Security
The GitHub Blog
The GitHub Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 【当耐特】
Jina AI
Jina AI
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
云风的 BLOG
云风的 BLOG
小众软件
小众软件
N
News and Events Feed by Topic

博客园 - 马伟

通过xrdp实现远程桌面连接Windows Azure linux虚拟机 使用VNC远程连接Windows Azure Linux虚拟机 设置Windows Azure Linux虚拟机中的root账户 使用Windows Azure PowerShell远程管理Windows Azure虚拟机 将SQL Azure数据库备份到本地SQL Server 2012 asp.net MVC3 “System.Web.Mvc.ModelClientValidationRule”问题 Session的生命周期 ASP.NET自定义输出缓存提供程序 ASP.NET缓存依赖--自定义缓存依赖 ASP.NET缓存依赖--SQL Server 2005与SQL Server 2008缓存依赖 QueryExtender控件之CustomExpression QueryExtender控件之OrderByExpression QueryExtender控件之PropertyExpression QueryExtender控件之RangeExpression QueryExtender控件之SearchExpession <<易学C#>>全书目录 用C#编程合并多个WORD文档 fckeditor编辑器上传文件出现invalid Request问题解决 荣获2009年“微软最有影响力开发者”
在ASP.NET中以编程方式设置母版页
马伟 · 2012-04-11 · via 博客园 - 马伟

2012-04-11 00:02  马伟  阅读(712)  评论()    收藏  举报

      在许多情况下,我们需要根据项目的运行情况,在页面运行时才决定使用哪个母版页。例如在企业管理系统中,我们要求公司的某个部门需要使用一个母版页,而其他部门则使用另外一个母版页。显然,这时候前面母版页调用方式是不能够满足的,它要求我们必须以编程方式来动态设置母版页。

      其实,通过编程方式来动态设置母版页非常方便。你只需设置Page.MasterPageFile属性就可以了。但这一步必须在Page.Init事件阶段完成,在这之后,再设置这一属性会产生一个异常。如下面的代码所示:

protected void Page_PreInit(object sender, EventArgs e)
     {
          Page.MasterPageFile = "~/Test.Master";
     }

       如果你将Page.MasterPageFile属性设置在Page_Load事件里,页面将会提示错误信息:“The 'MasterPageFile' property can only be set in or before the 'Page_PreInit' event.”。因此,你必须将Page.MasterPageFile属性设置在Page.Init事件里。

       在使用以编程方式来动态设置母版页时,还必须注意如下几点:

       1)确保在Web.config文件中或者内容页面的@Page指令中没有引用MasterPageFile的<pages>元素,只有这样才会得到成功加载的页面,并且引入了母版页。

       2)确保内容页面没有使用MasterType指令来创建对母版页的强类型引用。

       3)确保内容页面和所设置的的母版页完全兼容。