How to change theme of a total ASP.NET application dynamically?
Asked Answered
M

3

6

Imagine an ASP.NET application with several theme defined within it. How can I change theme of total application (not just a single page) dynamically. I know it is possible through <pages Theme="Themename" /> in web.config. But I want to be able to change it dynamically. How shpuld I do it?

Thanks in Advance

Mellott answered 23/6, 2010 at 6:46 Comment(0)
D
7

You can do this on Page_PreInit as explained 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;
    }
}
Duty answered 23/6, 2010 at 6:50 Comment(1)
@this. __curious_geek, why do prefer to do it in Page_Load not Pre_Int?Mellott
A
3

It is a very late answer, but I think you will like this..

You can change the Theme of the page in PreInit event, but you don't have use a base page..

In masterpage create a dropdown named ddlTema, after that write this code block in your Global.asax.. See how magic works :)

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;
                }

            };
    }
Alvie answered 11/4, 2012 at 14:40 Comment(0)
C
1

keep a common base page for all your asp.net pages and modify the theme property between any event after PreInit or before the Page_Load in the base page. This will force each page to apply that theme. As in this example make MyPage as base page for all your 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)
    {

    }
}
Courtenay answered 23/6, 2010 at 6:48 Comment(1)
Don't do this in Page_Load, but in PreInit.Paraformaldehyde

© 2022 - 2024 — McMap. All rights reserved.