Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change CSS Dynamically

Tags:

c#

css

asp.net

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.

Thanks in advance.

like image 828
Tarik Avatar asked Oct 01 '09 02:10

Tarik


2 Answers

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

like image 173
Tarik Avatar answered Nov 10 '22 10:11

Tarik


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>
like image 20
rick schott Avatar answered Nov 10 '22 09:11

rick schott