0

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;
        }
    }
}
musikluver2003
  • 165
  • 1
  • 9
  • Not an answer, but a suggestion: instead of the if-else-switch code, you can reduce this down to one line of code in the Page_PreInit method: `Page.Theme = Session["theme"].ToString() ?? "Blue";` – Boyd P Apr 19 '16 at 16:57

1 Answers1

0

Here is a good example of what you trying to achieve.

This answer can give you an idea how a postback generated by a dynamic control (drop down list in your case) will be processed by code behind.

This page provides several ways of how to program themes and change them dynamically.

Community
  • 1
  • 1
Red
  • 818
  • 2
  • 14
  • 26