想象一下,在其中定义了几个主题的ASP.NET应用程序。如何动态更改整个应用程序的主题(而不仅仅是单个页面)。我知道可以通过<pages Theme="Themename" />
中的web.config
。但我希望能够动态地改变它。怎么shpuld我这样做?
先谢谢
答案 0 :(得分:6)
您可以在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;
}
}
答案 1 :(得分:3)
这是一个非常晚的答案,但我想你会喜欢这个......
您可以在PreInit事件中更改页面的主题,但您没有使用基页..
在masterpage中创建一个名为ddlTema的下拉列表,然后在Global.asax中编写此代码块。看看魔法是如何工作的:)
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;
}
};
}
答案 2 :(得分:1)
为所有asp.net页面保留一个公共基页,并修改PreInit
之后或基页中Page_Load
之前的任何事件之间的主题属性。这将强制每个页面应用该主题。在本例中,将MyPage作为所有asp.net页面的基页。
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)
{
}
}