在运行时设置 ASP.NET 母版页
我正在开发一个需要能够支持两种或多种外观并在运行时可更改的网站。 我希望能够通过 CSS 开关来处理更改,但看起来我需要为每个设计使用不同的母版页。
那么,在运行时设置母版页的最佳方法是什么? Page.MasterPageFile 只能在Page.OnPreInit 事件中设置。 看起来解决方案是让我的所有页面继承处理 PreInit 事件的公共基础,或者使用执行此操作的 HttpModule。
有什么建议吗?
I'm working on a site which needs to be able to support two or more looks, changable at runtime. I'd hoped to be able to handle the change with a CSS switch, but it looks like I'll need to use a different masterpage for each design.
So, what's the best way to set the masterpage at runtime? Page.MasterPageFile can only be set in the Page.OnPreInit event. It looks like the solutions are to make all my pages inherit from a common base which handles the PreInit event, or to use an HttpModule which does that.
Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
与其使用两个不同的母版页,不如使用一个母版页来动态加载不同的用户控件和内容 HTML 文本?
Rather than two different master pages how about having one master that dynamically loads different user controls and content HTML literals?
我感受到你的痛苦。 我搜索了大约一个小时(如果不是更多的话)来解决这个问题,但没有成功。 当您有数百个页面时,“只需在每个页面上从 PreInit 调用它”并不是一个简单的答案。 但后来我意识到,我花在寻找解决方案上的时间比在每个页面上都花的时间要多。
但是,我想根据 Profile 属性设置 MasterPageFile,这样每页大约需要 5 行代码,这是可维护性的噩梦。 不管怎样,“不要重复自己”,对吧?
因此,我在 App_Code 文件夹的模块中创建了一个扩展方法,以使其更容易且更易于维护。
然后在每个页面的 PreInit 上,我只是这样调用:
I feel your pain. I searched for about an hour (if not more) for an issue to this, without success. It isn't just a cut and dry answer to say "just call it from PreInit on each page" when you have hundreds of pages. But then I realized that I was spending more time looking for a solution than it would have taken to just do it on each page.
However, I wanted to set the MasterPageFile based on a Profile property, so that would have been about 5 lines of code each page, a maintainability nightmare. And anyways, "don't repeat yourself", right?
So I created an Extension method in a module in the App_Code folder to make this easier and more maintainable.
And then on each page's PreInit, I just call this:
处理 PreInit 并插入加载正确母版页所需的一行代码非常容易。
如果没有令人信服的理由不走这条路,我就会这么做,无论您在哪里处理 PreInit。
It's easy enough to handle PreInit and insert the one line of code it takes to load the proper Master Page.
In the absence of some compelling reason not to go this route, that's what I'd do, regardless of where you handle the PreInit.
我很好奇是什么决定了页面的外观? 是用户点击按钮来更改主题吗? 是否基于用于访问该网站的 URL?
母版页支持后台代码,因此您可以在一个母版页中放入一些逻辑来决定应显示的内容。
我见过几个网站根据用户点击设置 cookie(以更改字体大小或页面宽度),然后根据这些 cookie 的值应用不同的 CSS 文件。 如果不存在 cookie,则显示默认外观。
编辑:
这里的另一个想法是,如果您只是尝试切换 CSS,则将样式标记设置为在服务器上运行,并在运行时为其分配属性。 这再次需要使用单个母版页,并将代码放在母版页的代码隐藏中,可能在 PreInit 事件处理程序中。
因为我从未实现过这个解决方案,所以我不确定整个 是否有效。 标签是否必须在服务器上运行。
I'm curious what decides how the page should look? Is it the user clicking a button to change the theme? Is it based on the URL that was used to get to the site?
Code behind is supported in Master Pages, so you could put some logic in your one Master Page to decide what should be displayed.
I've seen several sites set cookies based on user clicks (to change font size, or page width), and then have different CSS files applied based on the value of those cookies. If no cookie is present, display a default look and feel.
EDIT:
Another thought here, if you are simply trying to switch out CSS is to set your style tag to run at the server, and assign properties to it at run-time. Once again this would require the use of a single master page, and putting code the code-behind of the master page, probably in the PreInit event handler.
Since I've never implemented this solution I'm not sure if the whole <HEAD> tag has to run at the server or not.
从基类继承所有页面,例如
Inherit all you pages from a base class like
我以前已经这样做过一次,我完全按照您所描述的方式进行了操作(使所有页面都从具有 OnPreInit 事件的自定义页面继承)。 另外,我在 Global.asax.cs 中有一个自定义的 Application_PreRequestHandlerExecute 用于设置 Page.StyleSheetTheme 来进行不需要不同母版页的图像/css 更改。
I've done this once before, I did exactly what you described (Made all pages inherit from a custom page with an OnPreInit event). Also I had a custom Application_PreRequestHandlerExecute in my Global.asax.cs for setting Page.StyleSheetTheme for doing image/css changes that didn't require a different Master Page.