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
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;
}
}
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;
}
};
}
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)
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With