简体   繁体   中英

Changing stylesheet in code behind on masterpage

In maste page i set the stylesheet that defines the layout.

 <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.
I want to load a different css file when a specific parameter is passed to ShowDoc.aspx in query string.

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 ?

You could find the stylesheet link using the Master property on the ShowDoc page in Page_Load and redefine the Href property there.

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:

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; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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