I'm setting up my ASP.NET site to switch themes dynamically with a drop-down list. The themes are changing with every new selection, but the themes aren't matching up with the correct selection. In my App_Themes file, the theme folders are listed as: "Blue", "Gray" and "Green". With every new selection, the new applied theme just cycles through in that order, no matter which selection I make.

Example: The first time I pick a new theme, it will be blue. The second time it will be gray. The third is green and so on.

What am I doing wrong?

Default.aspx

<asp:DropDownList ID="ThemeList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ThemeList_SelectedIndexChanged">
    <asp:ListItem Value="Blue">Blue Theme</asp:ListItem>
    <asp:ListItem Value="Green">Green Theme</asp:ListItem>
    <asp:ListItem Value="Gray">Gray Theme</asp:ListItem>
</asp:DropDownList>

Default.aspx.cs

protected void ThemeList_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["theme"] = ThemeList.SelectedItem.Value;
}

protected void Page_PreInit(object sender, EventArgs e)
{
    if(Session["theme"] == null)
    {
        Page.Theme = "Blue";
    }

    else
    { 
        String chosenTheme = Session["theme"].ToString();

        switch (chosenTheme)
        {
            case "Blue":
                Page.Theme = "Blue";
                break;
            case "Green":
                Page.Theme = "Green";
                break;
            case "Gray":
                Page.Theme = "Gray";
                break;
            case "default":
                Page.Theme = "Blue";
                break;
        }
    }
}

Related posts

Recent Viewed