Thursday 31 July 2014

how to change css file at runtime

how to change css file at runtime



Add three CSS file as following

1)style1.css

body
{
    background-color:red;
}

2)style2.css

body
{
    background-color: green;

}


3)style3.css

body
{
    background-color:blue;
}


And your HTML is

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css" id="MyStyle" runat="server"  ></style>
    <%--<link href="style1.css" rel="stylesheet" type="text/css" />--%>
</head>
<body bgcolor="black"  >
    <form id="form1" runat="server">

    </form>
</body>
</html>









And copy following code in your code behind page

protected void Page_Load(object sender, EventArgs e)
    {

        //MyStyle.Attributes.Add("href", "style1.css");

        HtmlLink cssLink = new HtmlLink();
      
        if (DateTime.Now.Second < 20)
        {
            cssLink.Href = "style1.css";
        }

        if (DateTime.Now.Second >= 20 && DateTime.Now.Second < 40)
        {
            cssLink.Href = "style2.css";
        }
        if (DateTime.Now.Second >= 40 && DateTime.Now.Second <= 60)
        {
            cssLink.Href = "style3.css";
        }
        cssLink.Attributes.Add("rel", "stylesheet");
        this.Header.Controls.Add(cssLink);
    }
 

No comments:

Post a Comment



Asp.net tutorials