ASP.Net provides theme skins, which are actually skin changes. However, many people on the Internet say that this thing is useless, saying that this thing can be used in .aspx pages, but cannot be used in master pages (MasterPage). Yes, it is true that you cannot directly switch the theme of the entire website through a DDL selection in MasterPage, because the Page.Theme property is set in PreInit(){}, otherwise an error will be reported, and MasterPage will not execute PreInit().
I hope this example can help everyone solve this problem. In order for everyone to better understand my source code, I will briefly introduce it below.
First let’s talk about the idea: Our purpose is to put a DropDownList in the MasterPage, by switching the DropDownList (I do not use DropDownList here, but use Css to simulate a "DropDownList" in which a LinkButton is placed in each item, and each LinkButtond The value of PostBackUrl is in the form: "~/ThemeTool.aspx?Theme=theme name" ) to change the theme of the entire website, instead of just changing the current page, just like the one of Discuz!, so this change is global, But it is for the current thread, that is, user A changes the theme, which is only effective for user A and has no impact on user B, so we will not touch the configuration file. Then what we will quickly think of is session. Yes, we need to Put the name of the theme in the session, such as Session["Theme"]="Blue"; in this way, each page only needs to get the value of the Session in Page_PreInit() and assign it to the Page.Theme attribute of the page, and it will be OK. .
When we change the "DropDowList" option, here I actually click on a different Linkbutton, which means changing the name of the theme, which means changing the value of Session["Theme"]. This "change" Where to execute it? This is very critical. Is it in LinkButton_Click()? No, because reacquiring Page.Theme must be in Page_PreInit(), and LinkButton_Click() happens very late, after Load, let alone PreInit(), so I jump to ThemeTool here. Processed in .aspx, and then redirected to the original page after processing, so that the value of Session["Theme"] is changed before PreInit() of the original page. Please rest assured that this process will not cause ThemeTool to be loaded into the browser. What we can't see, we just borrowed its Page_PreInit() and left immediately after using it, haha! !
Okay, that’s the idea, let’s introduce the next three files:
1. MasterPage.master, what is worth mentioning in this file is my "DropDownList" simulated with CSS. Each item of the "DropDownList" has a LinkButton, and the value of each LinkButtond's PostBackUrl is in the form: " ~/ThemeTool.aspx?Theme=Theme name", so that every time the "DropDownList" option is changed, a new Theme value will be passed to ThemeTool.aspx, and then ThemeTool.aspx will process the new Theme value and store it in the Session. .
2. There is a ThemeSkin.cs file in the App_Code folder, which contains two static methods: GetTheme(Page P) and GetTheme(Page P). GetTheme(Page P) is used to get the theme. This method is on the website. It is called in Page_PreInit() of every page that needs to have a theme (I am here Default.aspx and Default2.aspx l). The parameter P refers to the page that calls the method, which is a Page type object; ChangeTheme() is used to Switching the theme is called in Page_PreInit() of ThemeTool.aspx.cs.
3. The ThemeTool.aspx page in the root directory is also very important. The theme switching is done in its Page_PreInit. Who told MasterPage not to execute PreInit? ^_^. The code inside is very simple. If you don’t believe me, take a look.
Expand