简体   繁体   English

更改母版页上的代码后面的样式表

[英]Changing stylesheet in code behind on masterpage

In maste page i set the stylesheet that defines the layout. 在maste页面中,我设置了定义布局的样式表。

 <link id="layoutStylesheet" href="CSS/Layout3Col.css" rel="stylesheet" type="text/css" runat="server" />

I have a ShowDoc.aspx page that inherits the master page. 我有一个继承母版页的ShowDoc.aspx页面。
I want to load a different css file when a specific parameter is passed to ShowDoc.aspx in query string. 我想在查询字符串中将特定参数传递给ShowDoc.aspx时加载不同的css文件。

How can I do it? 我该怎么做?
Should I define a public property in the master page so that showDoc.aspx can access it and change the layoutStylesheet ? 我应该在母版页中定义公共属性,以便showDoc.aspx可以访问它并更改layoutStylesheet吗?

You could find the stylesheet link using the Master property on the ShowDoc page in Page_Load and redefine the Href property there. 您可以使用Page_Load中ShowDoc页面上的Master属性找到样式表链接,并在那里重新定义Href属性。

HtmlLink link = Page.Master.FindControl( "layoutStyleSheet" ) as HtmlLink;
link.Href = ...your chosen stylesheet...

A bunch of different ways, but the simplest might be to just add this sort of code in Form_Load of your masterpage: 一堆不同的方法,但最简单的可能是在您的母版页的Form_Load中添加这种代码:

switch (Request["whateverstyle"]) {
    case "style1" : layoutStylesheet.Attributes["href"] = "style1.css";
    case "style2" : layoutStylesheet.Attributes["href"] = "style2.css";
    ...
}

Depending on how many pages you have that will want to change this, and how much else is changed at the same time, you may want to consider nested master pages. 根据您想要更改此页面的页数以及同时更改了多少其他页面,您可能需要考虑嵌套的母版页。

The root master page can define doctype/html/head/body and all the shared stuff; 根母版页可以定义doctype / html / head / body和所有共享内容; your "child" master pages can use that as their own master page. 您的“子”母版页可以将其用作自己的母版页。 Pages will use the child master pages only. 页面将仅使用子母版页。

Note that you can use ContentPlaceHolder controls outside of a Form, so you can put one in the HEAD element. 请注意,您可以在Form外部使用ContentPlaceHolder控件,因此可以将其放在HEAD元素中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM