Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 436
Afshar Mohebi Avatar asked Jun 23 '10 06:06

Afshar Mohebi


3 Answers

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;
    }
}
like image 158
Druid Avatar answered Oct 17 '22 18:10

Druid


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

            };
    }
like image 45
mtcakmak Avatar answered Oct 17 '22 16:10

mtcakmak


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)
    {

    }
}
like image 39
this. __curious_geek Avatar answered Oct 17 '22 17:10

this. __curious_geek