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

Last Reply on Mar 28, 2014 04:20 AM By Azim

Posted on Mar 28, 2014 12:15 AM

hello all i am currently working on a website and i want to be able to save my master page selected and theme selection as i tranfer through pages this is what i have for them so far

master -

    <asp:ContentPlaceHolder ID="RadContent" runat="server">
        <div id="btnRad">
            <asp:RadioButtonList ID="theme" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" OnSelectedIndexChanged="theme_SelectedIndexChanged" EnableViewState="true">
                <asp:ListItem Text="Theme1" Value="Style.css"></asp:ListItem>
                <asp:ListItem Text="Theme2" Value="main.css"></asp:ListItem>
            </asp:RadioButtonList>

            <asp:RadioButtonList ID="masters" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" OnSelectedIndexChanged="masters_SelectedIndexChanged" EnableViewState="true">
                <asp:ListItem>Master1</asp:ListItem>
                <asp:ListItem>Master2</asp:ListItem>
            </asp:RadioButtonList>
        </div>
    </asp:ContentPlaceHolder>

 

    protected void masters_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList rblmasters = (RadioButtonList)this.FindControl("RadioContent").FindControl("masters");

        if (rblmasters.SelectedIndex != ((int)Session["CurrentMaster"]))
        {
            Session["CurrentMaster"] = rblmasters.SelectedIndex;
            Server.Transfer(Request.FilePath, true);
        }
    }

 

    protected void theme_SelectedIndexChanged(object sender, EventArgs e)
    {
        linkCSS.Attributes["href"] = "~/Styles/" + this.theme.SelectedItem.Value;

    }

 

Posted on Mar 28, 2014 04:20 AM

You need to use Session for this and check session data in page load

Ref:

Dynamically change (switch) CSS file programmatically from code behind in ASP.Net

Master page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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>

 Master.cs

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

protected void masters_SelectedIndexChanged(object sender, EventArgs e)
{

}

Default.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Label ID="Label1" runat="server" Text="This is a Label" CssClass="label"></asp:Label>
    <asp:Button ID="Button1" Text="Second Page" OnClick="Redirect" runat="server" />
</asp:Content>

Default.aspx.cs

protected void Redirect(object sender, EventArgs e)
{
    Response.Redirect("~/Default2.aspx");
}

Default2.aspx:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Label ID="Label1" runat="server" Text="This is a Label" CssClass="label"></asp:Label>
    <asp:Button ID="Button1" Text="First Page" OnClick="Redirect" runat="server" />
</asp:Content>

Default2.aspx.cs

protected void Redirect(object sender, EventArgs e)
{
    Response.Redirect("~/Default.aspx");
}

Css1.css

body { font-family:Arial; font-size:10pt; }
.label { font-weight:bold; color:Red; }

Css2.css

body { font-family:Arial; font-size:10pt; }
.label {  font-weight:bold; color:Blue; }