Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday 6 July 2015

Introduction:
While working in asp.net, we need to change the style of the application dynamically. We can change the style sheet dynamically.  In this article I will explain how to change style sheet dynamically.

Implementation:
First of all create two or three style sheets in CSS folder of application. I have created three stylesheets here in this example as given below:
StyleSheet:
body {
    font-family:Arial;
    font-size:15px;
    }
.container
{
    background:#fff;
    color:#000;
    text-align:center;
    font-weight:bold;
    }
table {
  margin-left: 375px;
}


StyleSheet2:
body {
    font-family:Arial;
    font-size:15px;
    }
.container
{
    background:#0094ff;
    color:#fff;
    text-align:center;
     font-weight:bold;
    }
table {
  margin-left: 375px;
}


StyleSheet3:
body {
    font-family:Arial;
    font-size:15px;
    }
.container
{
    background:#ff0000;
    color:#fff;
    text-align:center;
    font-weight:bold;
    }
table {
  margin-left: 375px;
}


First StyleSheet is default Stylesheet of project/website.
In this example I have placed three buttons to perform the task.
Now design page as given below:
Design Section of WebPage:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <link href="css/StyleSheet.css" rel="stylesheet" id="linkstylesheet" runat="server"/>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div class="container">
        Change CSS or Style of the application or website dynamically
    </div>
        <table><tr><td>
            <asp:Button ID="btndefault" runat="server" Text="Use Default StyleSheet"/></td><td><asp:Button ID="btnstyle2" runat="server" Text="Use Second StyleSheet" /></td><td><asp:Button ID="btnstyle3" runat="server" Text="use 3rd StyleSheet"/></td></tr></table>
    </div>
    </form>
</body>
</html>


ASP.NET Code section:
Using C#:
protected void btndefault_Click(object sender, EventArgs e)
    {
        linkstylesheet.Href = "~/CSS/" + "StyleSheet.css";
    }
    protected void btnstyle2_Click(object sender, EventArgs e)
    {
        linkstylesheet.Href = "~/CSS/" + "StyleSheet2.css";
    }
    protected void btnstyle3_Click(object sender, EventArgs e)
    {
        linkstylesheet.Href = "~/CSS/" + "StyleSheet3.css";
    }


Using VB:
Protected Sub btndefault_Click(sender As Object, e As EventArgs) Handles btndefault.Click
        linkstylesheet.Href = "~/CSS/" + "StyleSheet.css"
    End Sub
    Protected Sub btnstyle2_Click(sender As Object, e As EventArgs) Handles btnstyle2.Click
        linkstylesheet.Href = "~/CSS/" + "StyleSheet2.css"
    End Sub
    Protected Sub btnstyle3_Click(sender As Object, e As EventArgs) Handles btnstyle3.Click
        linkstylesheet.Href = "~/CSS/" + "StyleSheet3.css"
    End Sub


Categories: , , ,

0 comments:

Post a Comment