3

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 ?

3 Answers 3

9

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...
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Is it efficient? Only one page of many that inherit this master page need to switch css, but with this every pafe will be checked.
0

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.

2 Comments

Only one page "showdoc.aspx", when "print version" is selected.
OK, probably not worth using nested master pages just for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.