This project is read-only.

Changing template dynamically

Topics: Developer Forum
Dec 6, 2007 at 12:53 PM
At some level in the site navigation, I want to automatically switch to another template - a sub-brand so to speak. Can anyone recommend and easy good approach?

Dec 14, 2007 at 9:02 PM
Edited Dec 14, 2007 at 9:04 PM
I'm working on an easy way to do this.

Add the following to WebPage.cs:

public string Theme
get { return _data.Theme; }
set { _data.Theme = value; }

And under WebPageData in the same file add Theme: Theme = string.Empty;
And the variable: public string Theme;

In navigation.aspx add a dropdownlist or something containing all avaliable themes, and add it in codebehind:
private void showPageDetails(string pageId)
myTheme.Text = page.Theme;

in btnNewPageClick and btnSaveClick set page.Theme = myTheme.Text

Now you have created all you need to insert the message, so its just to add the Theme to the page you are viewing

In PageBaseClass.cs just change the OnPreInit override:

protected override void OnPreInit(EventArgs e)
_website = WebSite.GetInstance();
string mytheme = string.Empty;
if (Request.QueryString"pg" != null)
WebPage page = new WebPage(Request.QueryString"pg");
mytheme = page.Theme;

if (mytheme == string.Empty)
if ((website.Theme == string.Empty) || (!Directory.Exists(Server.MapPath(string.Format("~/AppThemes/{0}", _website.Theme)))))
string appThemesPath = Server.MapPath("~/App_Themes");
string[] themes = Directory.GetDirectories(appThemesPath);
if (themes.Length > 0)
//if theme TravelDiary, select as default, else choose first theme
bool blnTravelDiary = false;
foreach (string theme in themes)
if (Path.GetFileName(theme) == "TravelDiary")
blnTravelDiary = true;
_website.Theme = blnTravelDiary == true ? "TravelDiary" : Path.GetFileName(themes0);
_website.Theme = string.Empty;
mytheme = _website.Theme;

Theme = mytheme;

a simple way to make themes spesific for pages in addition to the site :-)
Dec 15, 2007 at 11:00 PM
Edited Dec 16, 2007 at 8:15 PM
I did actually find an alternative way to do it, in evaluating the sitemap nodes to determine where in the navigational tree the user was and changing the template to suit depending on the navigational level, so home used default template but home > section2 (which is any area at this tier of navigation) used an alternate. Works a charm - its about six lines of code in PageBaseClass.cs but I need to be able to offer a simple way of allowing the user to select the template for a specific level, and I expect some of your example could be used.

When I get a chance I'll have a crack at adding your suggestions in so thanks for your effort as I think your approach on a page by page basis gives and increased level of flexibility over my own solution. A combination of the two would be very powerful indeed.