Limitation with different layouts

Topics: Developer Forum, Project Management Forum, User Forum
Jan 6, 2008 at 11:07 AM
I had several times got questions like adding different layouts for pages.
Eg. In a startpage I maby want just an image to be displayed, and in the second a page with a menu.

The startpage at http://www.hotellskonarum.se/ has an image instead of a menu on the left. Then go to http://www.hotellskonarum.se/boka-rum.aspx , there is now a menu on the left, this isn't possible to do in MWPSK without some changes.

I come up with a pretty simple solution to use different layout for pages.

Iv'e created extra masterpages that simply is a holder for a diffrent SkinID in the skin file.
In the master page I select the proper id for the page i want to use.
<cc1:TemplatedContent runat="server" ID="pnlTop" SkinID="Top"></cc1:TemplatedContent> Masterpage1
<cc1:TemplatedContent runat="server" ID="pnlTop" SkinID="TopTwo"></cc1:TemplatedContent> Masterpage2
<cc1:TemplatedContent runat="server" ID="pnlTop" SkinID="TopThree"></cc1:TemplatedContent> Masterpage3

In skinfile I add a new TemplatedContent section with a unique ID, see below
Page one
<cc1:TemplatedContent runat="server" SkinID="Top">

In second page
<cc1:TemplatedContent runat="server" SkinID="TopTwo"> <--NOTICE THE DIFFERENT SkinID

In third page
<cc1:TemplatedContent runat="server" SkinID="TopThree"> <--NOTICE THE DIFFERENT SkinID

In default.aspx.cs in PreInit section I look for the page id that is posted, and setup the masterpage that refer to that page.
Of course you have to add this variable to the WebPage.cs to make this work and have a selection of the masterpage you want to use.
public string PageTheme
{
get { return _data.PageTheme; }
set { _data.PageTheme = value; }
}

if (Request.QueryString"pg" != null)
{
_page = new WebPage(Request.QueryString"pg");

if ((Request.QueryString"pg" == page.PageId) && (page.PageTheme != string.Empty))
{
this.Page.MasterPageFile = _page.PageTheme;
}
else
{
this.Page.MasterPageFile = "~/Site.master";
}
}
else
{
//The homepage id is not inited, create a new instance
_website = WebSite.GetInstance();
if (_website != null)
{
page = new WebPage(website.HomepageId);
if (_page.PageTheme != string.Empty)
{
this.Page.MasterPageFile = _page.PageTheme;
}
else
{
this.Page.MasterPageFile = "~/Site.master";
}

}else{
this.Page.MasterPageFile = "~/Site.master";
}
}

You maby wonder why we should have different layouts for pages. Well we can add controls that we like in this solution, and just not use the same for all pages.
Maby the TemplatedContent class should be rebuilt with a new file that is a holder that takes an argument from a configfile for the controls and html for each layout.
Feb 10, 2008 at 4:43 PM
Exaclty what do I need to add in the WebPage.cs to make this work?

Or even better, is it possible to get a step by step guide for this, or to get access to the source files with comments?

I need to set up a different layout for the startpage, but this sullotion not clear enough for me... as me being a kind of a newbie to programming.
Feb 11, 2008 at 8:41 AM

In WebPage.cs add:
public string PageTheme
{
get { return _data.PageTheme; }
set { _data.PageTheme = value; }
}

In WebPageData (Inner class of WebPage.cs)

Add following lines:
public string PageTheme;
PageTheme = string.Empty;

This enables you to get and set the page theme of a page
Feb 11, 2008 at 5:55 PM
Hi richard, could you possibly post more precise instructions for making your modifcations? Even better if you could upload the files with the code modifcations; remarked of course.
Feb 11, 2008 at 7:23 PM
OK, great thanks for the help Rickard, I'll give it a new try.
Feb 16, 2008 at 12:54 PM


rickardmagnusson wrote:

In WebPage.cs add:

In WebPageData (Inner class of WebPage.cs)

Add following lines:
public string PageTheme;
PageTheme = string.Empty;

This enables you to get and set the page theme of a page


I get error indication in Visual studio when I add the code within the WebPageData class, in the file WebPage.cs...
The error message is: "Invalid token in class, struct or interface or declaration"
I get the message when hovering over the signs: = . ;
This is in the line: PageTheme = string.Empty;


I made the changes as below...

/// <summary>
/// Data-Class for the WebPage
/// </summary>
public class WebPageData
{

public WebPageData()
{

Visible = true;
AllowAnonymousAccess = false;
VirtualPath = string.Empty;
NavigationName = string.Empty;
Title = string.Empty;
Sections = new List<string>();

}
//Added PageTheme...
public string PageTheme;
PageTheme = string.Empty;

public bool Visible ;
etc...
Feb 16, 2008 at 1:23 PM
I still dont understand where the code should be added into the default.aspx.cs file?
I've tried to add it as below in between my comments, but it doesn't function:

//PAGE initiation object...
//This is where to add code for being able to choose new masterpages.

protected void Page_Init(object sender, EventArgs e)
{
if (Request.QueryString"pg" != null)
{
_page = new WebPage(Request.QueryString"pg");

//ADD NEW CODE HERE???
if ((Request.QueryString"pg" == page.PageId) && (page.PageTheme != string.Empty))
{
this.Page.MasterPageFile = _page.PageTheme;

etc...

//END NEW CODE


if ((_page.AllowAnonymousAccess == false) && (User.Identity.IsAuthenticated == false))
page = new WebPage(website.HomepageId);

Feb 17, 2008 at 3:06 PM


Amani wrote:
I still dont understand where the code should be added into the default.aspx.cs file?
I've tried to add it as below in between my comments, but it doesn't function:

//PAGE initiation object...
//This is where to add code for being able to choose new masterpages.

protected void Page_Init(object sender, EventArgs e)
{
if (Request.QueryString"pg" != null)
{
_page = new WebPage(Request.QueryString"pg");

//ADD NEW CODE HERE???
if ((Request.QueryString"pg" == page.PageId) && (page.PageTheme != string.Empty))
{
this.Page.MasterPageFile = _page.PageTheme;

etc...

//END NEW CODE


if ((_page.AllowAnonymousAccess == false) && (User.Identity.IsAuthenticated == false))
page = new WebPage(website.HomepageId);





Instead of adding it to default.aspx, add it to PageBaseClass.cs

/// <summary>
/// Set's a Masterpage for this page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_PreInit(object sender, EventArgs e)
{
if (Request.QueryString"pg" != null)
{
_page = new WebPage(Request.QueryString"pg");

if ((Request.QueryString"pg" == page.PageId) && (page.PageTheme != string.Empty))
{
this.Page.MasterPageFile = _page.PageTheme;
}
else
{
this.Page.MasterPageFile = "~/Site.master";
}
}
else
{
//The homepage id is not inited, create a new instance
_website = WebSite.GetInstance();
if (_website != null)
{
page = new WebPage(website.HomepageId);
if (_page.PageTheme != string.Empty)
{
this.Page.MasterPageFile = _page.PageTheme;
}
else
{
this.Page.MasterPageFile = "~/Site.master";
}
}
else
{
this.Page.MasterPageFile = "~/Site.master";
}
}
}
Feb 17, 2008 at 3:11 PM


Amani wrote:


rickardmagnusson wrote:

In WebPage.cs add:

In WebPageData (Inner class of WebPage.cs)

Add following lines:
public string PageTheme;
PageTheme = string.Empty;

This enables you to get and set the page theme of a page


I get error indication in Visual studio when I add the code within the WebPageData class, in the file WebPage.cs...
The error message is: "Invalid token in class, struct or interface or declaration"
I get the message when hovering over the signs: = . ;
This is in the line: PageTheme = string.Empty;


I made the changes as below...

/// <summary>
/// Data-Class for the WebPage
/// </summary>
public class WebPageData
{

public WebPageData()
{

Visible = true;
AllowAnonymousAccess = false;
VirtualPath = string.Empty;
NavigationName = string.Empty;
Title = string.Empty;
Sections = new List<string>();

}
//Added PageTheme...
public string PageTheme;
PageTheme = string.Empty;

public bool Visible ;
etc...




In WebPage.cs add as follow:

public class WebPageData
{
public string PageTheme;

public WebPageData()
{
PageTheme = string.Empty;
}


}
Mar 1, 2008 at 5:31 PM
OK I still dont get this to work. I belive I have done everything as U say Rickard.
But still I get a compilation error in the PageBaseClass.cs.

The three errors I get are for every line of (Request.QueryString"pg" != null)
The errors I get are...
) expected
;expected
Invalid expression term ')'

I have inserted the code to set a Masterpage AFTER the line 23 public PageBaseClass(){}

...and BEFORE the line that says:
protected override void OnPreInit(EventArgs e) (line )

Apart from this I also get an error for the new secondary Master-file that I have created saying:
Element 'TemplatedContent' is not a known element. This can occur if there is a compilation error in the Web site.

Please, is it possible that anyone simply upload the source files? Or I am prepared to pay for them... I just want to finish with this stuff as soon as possible.

Mar 1, 2008 at 5:43 PM
Oh BTW I forgot to mention that I also get an error saying:
Invalid expression term 'else'

And this happens for the following lines that I have added in PageBaseClass.cs...

else
{
this.Page.MasterPageFile = "~/Site.master";
}
}
else
{

Mar 2, 2008 at 7:22 AM


Amani wrote:
Oh BTW I forgot to mention that I also get an error saying:
Invalid expression term 'else'

And this happens for the following lines that I have added in PageBaseClass.cs...

else
{
this.Page.MasterPageFile = "~/Site.master";
}
}
else
{




Hello
It sound like you forgoten to close a tag or something. I send you the code.
Here is my PageBaseClass.cs OnPreInit. Hope this will help you.

protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
_website = WebSite.GetInstance();

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);
}
else
{
_website.Theme = string.Empty;
}
_website.SaveData();
}
Theme = _website.Theme;

//Select masterpage
if (Request.QueryString"pg" != null)
{
_page = new WebPage(Request.QueryString"pg");

if ((Request.QueryString"pg" == page.PageId) && (page.PageTheme != string.Empty))
{
this.Page.MasterPageFile = _page.PageTheme;
}
else
{
this.Page.MasterPageFile = "~/Site.master";
}
}
else
{
//The homepage id is not inited, create a new instance
_website = WebSite.GetInstance();
if (_website != null)
{
page = new WebPage(website.HomepageId);
if (_page.PageTheme != string.Empty)
{
this.Page.MasterPageFile = _page.PageTheme;
}
else
{
this.Page.MasterPageFile = "~/Site.master";
}

}
else
{
this.Page.MasterPageFile = "~/Site.master";
}
}

}



Also update Administration/Navigation.aspx.cs in "showPageDetails" (Automatically creates config file)

/PageThemes/

//Create PageThemes.config if not exist
if (!File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/PageThemes.config")))
{
MyWebPagesStarterKit.PageTheme themefile = new Medio.PageTheme();
themefile.SaveData();
}

MyWebPagesStarterKit.PageTheme theme = MyWebPagesStarterKit.PageTheme.GetInstance();

drpPageTheme.Items.Clear();
drpPageTheme.DataSource = theme.PageThemeEntries;
drpPageTheme.DataBind();

//Insert an emty theme for unselecting a theme
drpPageTheme.Items.Insert(0, new ListItem("No theme",""));

//Selecting the theme for this page
if(page.PageTheme != string.Empty & page.PageTheme != null)
drpPageTheme.Items.FindByValue(page.PageTheme.ToString()).Selected = true;

/End PageThemes/
Mar 2, 2008 at 7:42 AM


Amani wrote:
OK I still dont get this to work. I belive I have done everything as U say Rickard.
But still I get a compilation error in the PageBaseClass.cs.

The three errors I get are for every line of (Request.QueryString"pg" != null)
The errors I get are...
) expected
;expected
Invalid expression term ')'

I have inserted the code to set a Masterpage AFTER the line 23 public PageBaseClass(){}

...and BEFORE the line that says:
protected override void OnPreInit(EventArgs e) (line )

Apart from this I also get an error for the new secondary Master-file that I have created saying:
Element 'TemplatedContent' is not a known element. This can occur if there is a compilation error in the Web site.

Please, is it possible that anyone simply upload the source files? Or I am prepared to pay for them... I just want to finish with this stuff as soon as possible.




Look in:
protected string DefaultMaster() In PageThemeAdmin.aspx.cs
Change the line s+="<%@ Register TagPrefix=\"cc1\" Namespace=\"Medio.Controls\" %>\n";
To
s+="<%@ Register TagPrefix=\"cc1\" Namespace=\"MyWebPagesStarterKit.Controls\" %>\n";
Mar 2, 2008 at 10:03 AM
Hello thanks for your answers! :-)
But I dont find any file named PageThemeAdmin.aspx.cs in any of the MWPSK folders.



Look in:
protected string DefaultMaster() In PageThemeAdmin.aspx.cs
Change the line s+="<%@ Register TagPrefix=\"cc1\" Namespace=\"Medio.Controls\" %>\n";
To
s+="<%@ Register TagPrefix=\"cc1\" Namespace=\"MyWebPagesStarterKit.Controls\" %>\n";

Mar 2, 2008 at 10:57 AM


Amani wrote:
Hello thanks for your answers! :-)
But I dont find any file named PageThemeAdmin.aspx.cs in any of the MWPSK folders.



Look in:
protected string DefaultMaster() In PageThemeAdmin.aspx.cs
Change the line s+="<%@ Register TagPrefix=\"cc1\" Namespace=\"Medio.Controls\" %>\n";
To
s+="<%@ Register TagPrefix=\"cc1\" Namespace=\"MyWebPagesStarterKit.Controls\" %>\n";

See post http://www.codeplex.com/MyWebPagesStarterKit/WorkItem/View.aspx?WorkItemId=15307

Mar 2, 2008 at 3:17 PM

rickardmagnusson wrote:
See post http://www.codeplex.com/MyWebPagesStarterKit/WorkItem/View.aspx?WorkItemId=15307

OK, thanks. I am getting there... But I still get an error after setting up the files and changing the namespaces...

In the file pageBaseClass.cs I get this error:
"The name '_page' does not exist in the current context"

That I get for the piece of code that is inserted after line 53 Theme = _website.Theme; and begins with the line:
if (Request.QueryString["pg"] != null)
Mar 3, 2008 at 6:48 AM


Amani wrote:

rickardmagnusson wrote:
See post http://www.codeplex.com/MyWebPagesStarterKit/WorkItem/View.aspx?WorkItemId=15307

OK, thanks. I am getting there... But I still get an error after setting up the files and changing the namespaces...

In the file pageBaseClass.cs I get this error:
"The name '_page' does not exist in the current context"

That I get for the piece of code that is inserted after line 53 Theme = _website.Theme; and begins with the line:
if (Request.QueryString["pg"] != null)



You need to declare _page: "protected WebPage _page;"
Mar 3, 2008 at 7:21 PM
OK, thanks Rickard!
That solved the problem. Finally it's working! :-)



You need to declare _page: "protected WebPage _page;"

Mar 8, 2008 at 5:33 PM
I dont get the selection of Masterpages to "be accepted".
I have followed the instructions in the file Navigation.txt to be found here...

rickardmagnusson wrote:
http://www.codeplex.com/MyWebPagesStarterKit/WorkItem/View.aspx?WorkItemId=15307


...BUT STILL it does not work. I dont get any error messages in VS 2005. I can create and see new the custom masterpages using the Admin page.

It seems that the webforms are never being asociated with any other Masterpage than the default Site.master.

Ideas/help anyone?

Am I for example supposed to make any changes in the default.aspx.cs?

Is it possible to "hard code" the selection of Masterpage instead of doing it usng the Admin page? Since I anyway just want to have the startpage of the site to use a differenmt Masterpage, that would be a OK sollution, even if its not the optimal one...
Mar 9, 2008 at 7:47 AM


Amani wrote:
I dont get the selection of Masterpages to "be accepted".
I have followed the instructions in the file Navigation.txt to be found here...

rickardmagnusson wrote:
http://www.codeplex.com/MyWebPagesStarterKit/WorkItem/View.aspx?WorkItemId=15307


...BUT STILL it does not work. I dont get any error messages in VS 2005. I can create and see new the custom masterpages using the Admin page.

It seems that the webforms are never being asociated with any other Masterpage than the default Site.master.

Ideas/help anyone?

Am I for example supposed to make any changes in the default.aspx.cs?

Is it possible to "hard code" the selection of Masterpage instead of doing it usng the Admin page? Since I anyway just want to have the startpage of the site to use a differenmt Masterpage, that would be a OK sollution, even if its not the optimal one...


Hi again

Look in Dataclass of Webpage (guid.config) and see if there's a node "<PageTheme />" and that it has a value. If it has a value, something is wrong in PageBaseClass.cs
You can provide static masterpages in Navigation.aspx.cs Like: drpPageTheme.Items.Insert(1, new ListItem("MasterpageTitle","PathToMasterpage"));
Mar 9, 2008 at 9:31 AM
Edited Mar 9, 2008 at 9:33 AM

Look in Dataclass of Webpage (guid.config) and see if there's a node "<PageTheme />" and that it has a value. If it has a value, something is wrong in PageBaseClass.cs


Hello and thanks again,
but I dont have any file named guid.config in my project.
Where can I find that file, or can you please post its content? From what I can see it was not included in the PageTheme.rar nor in the MWPSK package.
Oh and and in what folder should it be?
Mar 9, 2008 at 11:43 AM

but I dont have any file named guid.config in my project.
Where can I find that file, or can you please post its content? From what I can see it was not included in the PageTheme.rar nor in the MWPSK package.
Oh and and in what folder should it be?

What rickardmagnusson meant is not a file named literally "guid.config", but the .config files inside App_Data/Pages, which are named with the individual pages' GUID, for example: "30eb6284-3c67-4452-9065-825774a4c6e1.config".
Mar 9, 2008 at 11:58 AM

What rickardmagnusson meant is not a file named literally "guid.config", but the .config files inside App_Data/Pages, which are named with the individual pages' GUID, for example: "30eb6284-3c67-4452-9065-825774a4c6e1.config".


OK, yes I have thoose files. And they have the node "<PageTheme />" and without any value.
Mar 9, 2008 at 10:25 PM


Amani wrote:

What rickardmagnusson meant is not a file named literally "guid.config", but the .config files inside App_Data/Pages, which are named with the individual pages' GUID, for example: "30eb6284-3c67-4452-9065-825774a4c6e1.config".


OK, yes I have thoose files. And they have the node "<PageTheme />" and without any value.


Ok. Have you followed the other steps and added the functionallity to add a pagetheme to a page in Navigation.aspx (and .cs)?
If you like to test if the pagebaseclass servs a masterpage for a single page, for eg. default.aspx(your startpage). Add in the guid.config: <PageTheme >~/PathTo_Your_Masterpage/mymasterpage.master<PageTheme >
Mar 10, 2008 at 6:53 PM
Yes I can select a different Masterpage when I change it in the PageThemes.config file as you say. <PageTheme >~/PathTo_Your_Masterpage/mymasterpage.master<PageTheme >.
But I cant understand why it doesnt work to to use the admin page (Navigation.aspx). I think I have followed all steps. At least I have double checked it over and over.

I may also add that the button Create data in file is not active on my PageThemeAdmin.aspx file.

And BTW... I get a warning (not error message) when building the site, maybe it has a connection to the problem?:

'AdministrationWebSite.PagePreRender(object, System.EventArgs)' hides inherited member 'MyWebPagesStarterKit.Controls.PageBaseClass.PagePreRender(object, System.EventArgs)'. Use the new keyword if hiding was intended. C:\Documents and,,,\...\Visual Studio 2005\Templates\ProjectTemplates\....\Administration\WebSite.aspx.cs_


richardmagnusson wrote:
Ok. Have you followed the other steps and added the functionallity to add a pagetheme to a page in Navigation.aspx (and .cs)?
If you like to test if the pagebaseclass servs a masterpage for a single page, for eg. default.aspx(your startpage). Add in the guid.config: <PageTheme >~/PathTo_Your_Masterpage/mymasterpage.master<PageTheme >

Mar 11, 2008 at 11:25 AM


Amani wrote:
Yes I can select a different Masterpage when I change it in the PageThemes.config file as you say. <PageTheme >~/PathTo_Your_Masterpage/mymasterpage.master<PageTheme >.
But I cant understand why it doesnt work to to use the admin page (Navigation.aspx). I think I have followed all steps. At least I have double checked it over and over.

I may also add that the button Create data in file is not active on my PageThemeAdmin.aspx file.

And BTW... I get a warning (not error message) when building the site, maybe it has a connection to the problem?:

'AdministrationWebSite.PagePreRender(object, System.EventArgs)' hides inherited member 'MyWebPagesStarterKit.Controls.PageBaseClass.PagePreRender(object, System.EventArgs)'. Use the new keyword if hiding was intended. C:\Documents and,,,\...\Visual Studio 2005\Templates\ProjectTemplates\....\Administration\WebSite.aspx.cs_


richardmagnusson wrote:
Ok. Have you followed the other steps and added the functionallity to add a pagetheme to a page in Navigation.aspx (and .cs)?
If you like to test if the pagebaseclass servs a masterpage for a single page, for eg. default.aspx(your startpage). Add in the guid.config: <PageTheme >~/PathTo_Your_Masterpage/mymasterpage.master<PageTheme >


I downloaded a new fresh copy of 'MyWebPagesStarterKit and implemented the pagetheme.
I uploaded a complete solution to http://www.medio.se/files/MyWebPagesStarterKit_1.1.4.rar

Mar 11, 2008 at 9:20 PM

I downloaded a new fresh copy of 'MyWebPagesStarterKit and implemented the pagetheme.
I uploaded a complete solution to http://www.medio.se/files/MyWebPagesStarterKit_1.1.4.rar


OK thanks, great! I will try it when I have the time.
Jul 30, 2008 at 7:01 PM
Hi folks!
Have a look here: http://www.codeplex.com/MyWebPagesStarterKit/WorkItem/View.aspx?WorkItemId=17599
Just another approach to changing layout and look & feel for selected pages.
Cheers,
Mark