Anyone Setting Themes from Master Page

  • Thread starter Thread starter Jonathan Wood
  • Start date Start date
J

Jonathan Wood

I want to dynamically set my site's theme based on a setting stored in my
database.

I just hooked this up but get an error that the theme can only be set in the
page's PreInit event or earlier. However, it appears that Master pages do
not have PreInit events.

I REALLY do not want to have to stick the same code in each and every page I
create. How are you folks dynamically setting themes from master pages?

Thanks.

Jonathan
 
You could make every page inherit from a base class with the code you want
in the pre init event.
 
Check out K. Scott Allen's great articles with good sample code :

http://odetocode.com/Blogs/scott/archive/2005/12/09/2604.aspx
http://www.odetocode.com/Articles/450.aspx

Rick Strahl has an alternate method :
http://www.west-wind.com/WebLog/posts/4899.aspx

Also, you could set the global theme in web.config:

<configuration>
<system.web>
<pages theme="ThemeName" />
</system.web>
</configuration>




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
It's a reasonable suggestion, but would still require that I go back and
modify each and every page created so far. I'll leave this as second choice
but am looking into more of a global handler approach if possible.

Jonathan
 
Juan,

Thanks, these look like good references.
Also, you could set the global theme in web.config:

<configuration>
<system.web>
<pages theme="ThemeName" />
</system.web>
</configuration>

I'm doing this now. But this doesn't help me change it dynamically.

Jonathan
 
There is no work-around in this context Jonathan. We MUST use a base class
that each page inherits from.
 
Actually... that's not quite true. In fact, I was able to implement my own
IHttpModule class that handles themes for all pages globally, without
needing to modify each page.

Jonathan
 
Back
Top