views:

21

answers:

2

Hi all,

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

+1  A: 

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

    }
}
this. __curious_geek
Don't do this in Page_Load, but in `PreInit`.
Jan Jongboom
right. Updated the answer. Thanks.
this. __curious_geek
+3  A: 

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;
    }
}
Druid
@this. __curious_geek, why do prefer to do it in Page_Load not Pre_Int?
afsharm