Actually, it must be set to Page_PreInit , it will not work if you try to change the theme in Page_Init .
The most common solution is to use a parent class for all of your pages. This is a one-time change and puts the logic in the parent class. Instead of inheriting from Page you inherit, say, ThemedPage . Inside the ThemedPage class, which naturally inherits from Page , you can override the Page.OnPreInit method.
You asked for “two lines,” this is actually one if you remove the clutter. This is VB:
Public Class ThemedPage Inherits Page Protected Overrides Sub OnPreInit(ByVal e As System.EventArgs) Me.Theme = HttpContext.Current.Request.Url.Host.Replace(".com", "") MyBase.OnPreInit(e) End Sub End Class
And instead:
Partial Class _Default Inherits System.Web.UI.Page
Now you write this:
Partial Class _Default Inherits ThemedPage
It's all! One-time search / replacement, and you're done. For completeness, here is the same (only class) for C #:
// C
Update: added sample VB code
Update: added C # code sample
Note. the theme must exist, otherwise you will get an exception: Theme 'ThemeName' cannot be found in the application or global theme directories. . If you want to use the default theme or not, if the theme is not there, wrap it around the try / catch and use the catch to set the default theme.
source share