Dynamically change Theme by changing CSS File in Master Page in ASP.Net

Last Reply on Mar 23, 2014 12:57 AM By Azim

Posted on Mar 23, 2014 12:05 AM

hello all

I have made a master page along with a content page

i have some radio buttons and with these radio buttons i want to be able to switch style sheets

 

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
        <div id="btnRadio">
            <asp:RadioButtonList ID="themes" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" OnSelectedIndexChanged="themes_SelectedIndexChanged" >
                <asp:ListItem Text="Theme1"></asp:ListItem>
                <asp:ListItem>Theme2</asp:ListItem>
            </asp:RadioButtonList>
            <asp:RadioButtonList ID="masters" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" OnSelectedIndexChanged="masters_SelectedIndexChanged">
                <asp:ListItem>Master1</asp:ListItem>
                <asp:ListItem>Master2</asp:ListItem>
            </asp:RadioButtonList>
        </div>
</asp:Content>

in my master page i have this

 

<head runat="server">
    <title>Welcome</title>

    <link href="Styles/welcomeStyle.css" rel="stylesheet" type="text/css" media="all" id="stylesheet" />

</head>

 

Posted on Mar 23, 2014 12:57 AM

Ref:

 
Master Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link id="lnkCSS" runat="server" href="~/CSS/Css1.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="btnRadio">
            <asp:RadioButtonList ID="rblThemes" runat="server" RepeatDirection="Horizontal" AutoPostBack="true"
                OnSelectedIndexChanged="themes_SelectedIndexChanged">
                <asp:ListItem Text="Theme1" Value="Css1.css"></asp:ListItem>
                <asp:ListItem Text="Theme2" Value="Css2.css"></asp:ListItem>
            </asp:RadioButtonList>
            <asp:RadioButtonList ID="masters" runat="server" RepeatDirection="Horizontal" AutoPostBack="true"
                OnSelectedIndexChanged="masters_SelectedIndexChanged">
                <asp:ListItem>Master1</asp:ListItem>
                <asp:ListItem>Master2</asp:ListItem>
            </asp:RadioButtonList>
        </div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

 

protected void themes_SelectedIndexChanged(object sender, EventArgs e)
{
    lnkCSS.Attributes["href"] = "~/CSS/" + this.rblThemes.SelectedItem.Value;
}

protected void masters_SelectedIndexChanged(object sender, EventArgs e)
{

}

Default.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Label ID="Label1" runat="server" Text="This is a Label" CssClass="label"></asp:Label>
</asp:Content>

Image:

Thank You.