Afshar
Afshar

Reputation: 11483

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

Upvotes: 6

Views: 9021

Answers (3)

mtcakmak
mtcakmak

Reputation: 31

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

            };
    }

Upvotes: 3

this. __curious_geek
this. __curious_geek

Reputation: 43207

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

    }
}

Upvotes: 1

Druid
Druid

Reputation: 6453

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

Upvotes: 7

Related Questions