How to dynamically change the theme of an ASP.NET application?

Imagine an ASP.NET application with several topics defined inside it. How can I dynamically change the theme of a common application (not just one page). I know this is possible through <pages Theme="Themename" />at web.config. But I want to be able to change it dynamically. How shpuld am i doing this?

Thanks at Advance

+5
source share
3 answers

You can do this on Page_PreInit as described here :

protected void Page_PreInit(object sender, EventArgs e)
{
    switch (Request.QueryString["theme"])
    {
        case "Blue":
            Page.Theme = "BlueTheme";
            break;
        case "Pink":
            Page.Theme = "PinkTheme";
            break;
    }
}
+6
source

This is a very late answer, but I think you will like it.

You can change the page theme in the PreInit event, but you are not using the base page.

ddlTema, Global.asax.. , :)

public class Global : System.Web.HttpApplication
{

    protected void Application_PostMapRequestHandler(object sender, EventArgs e)
    {
        Page activePage = HttpContext.Current.Handler as Page;
        if (activePage == null)
        {
            return;
        }
        activePage.PreInit
            += (s, ea) =>
            {

                string selectedTheme = HttpContext.Current.Session["SelectedTheme"] as string;
                if (Request.Form["ctl00$ddlTema"] != null)
                {
                    HttpContext.Current.Session["SelectedTheme"]
                        = activePage.Theme = Request.Form["ctl00$ddlTema"];
                }
                else if (selectedTheme != null)
                {
                    activePage.Theme = selectedTheme;
                }

            };
    }
+3

keep a common base page for all your asp.net pages and change the theme property between any event after PreInitor before Page_Loadon the base page. This will force every page to apply this theme. As in this example, make MyPage the base page for the entire asp.net page.

public class MyPage : System.Web.UI.Page
{
    /// <summary>
    /// Initializes a new instance of the Page class.
    /// </summary>
    public Page()
    {
        this.Init += new EventHandler(this.Page_Init);
    }


    private void Page_Init(object sender, EventArgs e)
    {
        try
        {
            this.Theme = "YourTheme"; // It can also come from AppSettings.
        }
        catch
        {
            //handle the situation gracefully.
        }
    }
}

//in your asp.net page code-behind

public partial class contact : MyPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
+1
source

All Articles