ASP.NET 动态更改母版页
是否可以通过单击内容页上的按钮来更改内容页的母版页?
如果不是为什么?
Is it possible to change the master-page of a content-page with the click of a button on that content-page?
If not why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有可能,您必须像这样重写代码隐藏类的 OnPreInit 方法...
因此,要将其绑定到单击,您可以使用查询字符串参数,即
然后在代码隐藏中
It is possible, you'll have to override the OnPreInit method of your codebehind class like so...
So to bind this to a click, you could use a query string parameter, i.e.
And then in the codebehind
您可以通过编程方式设置母版页,但是只能在预初始化事件中执行此操作。
http://odetocode.com/articles/450.aspx
You can set the master page programmatically, however you can only do it in the pre-init event.
http://odetocode.com/articles/450.aspx
您可以拥有一个常规的非服务器
您不能将服务器端表单与常规按钮事件一起使用,因为它们在页面生命周期中触发得太晚。
You can have a regular, non-server
<form>
, with a hidden<input>
field. When the form posts, you check for the<input>
value in thePre_Init
event, and change the Master Page there.You can't use a server-side form with a regular button event, because they fire too late in the page life cycle.
我最近这样做了,我根据正在渲染的页面更改了母版页上的图像。
1) 我引用了控件(母版页上的 imgPageSpecificTextImg)
2) 更改了代码指向的 URL。
I did this recently where I changed an image on the masterpage based on the page that was being rendered.
1) I referenced the control (imgPageSpecificTextImg on the Masterpage)
2) Changed the URL that the code was pointing to.