7

I need to determine which CSS should be applied on some specific pages. I have a master page which has children and default.aspx and services.aspx are ones of the children of Master page. What I want to is when user navigates Default.aspx or Services.aspx,system should apply DefaultCSS file otherwise I want to apply some ordinary css file.

How can I do that and for this question what kind of practice would be better.

4 Answers 4

9

I think I've found what I am looking for :

protected void Page_Init(object sender, EventArgs e)
    {
        HtmlLink css = new HtmlLink();
        css.Href = "css/fancyforms.css";
        css.Attributes["rel"] = "stylesheet";
        css.Attributes["type"] = "text/css";
        css.Attributes["media"] = "all";
        Page.Header.Controls.Add(css);
    }

Also MSDN was describing how to achieve this : HtmlLink Class

Sign up to request clarification or add additional context in comments.

Comments

7

Its much easier and more flexible to do this:

MasterPage:

<head>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>

Child-Page 1:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="css/fancyforms.css" rel="stylesheet" type="text/css" />
</asp:Content>

Child-Page 2:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="css/NOTfancyforms.css" rel="stylesheet" type="text/css" />
</asp:Content>

2 Comments

Yeap, you are right but I don't know I just wanted to get fancy about that but I liked your solution more than mine.
The benefit of this approach is that if you change your .css, .js, meta tags...whatever, you don't have to re-compile anything. You will also get design-time support in Visual Studio for your .aspx.
2

You might want to investigate using themes as well.

http://msdn.microsoft.com/en-us/library/ykzx33wh.aspx

Comments

1

You can include the CSS file declaratively, i.e. in the *.aspx file: see for example this answer.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.