lupital
lupital

Reputation:

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 ?

Upvotes: 3

Views: 7495

Answers (3)

Dave Roberts
Dave Roberts

Reputation: 537

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.

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532445

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

Upvotes: 9

Dave Markle
Dave Markle

Reputation: 97671

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

Upvotes: 0

Related Questions