Welcome to MSDN Blogs Sign in | Join | Help

Cristiano WebLog

Microsoft Infrastructure & Development Tips and Tricks
The Page Appearance Framework.

Are you curious on how to control the appearance of an ASP .Net pages using design time properties? Guys from Commerce Server team have shown how to deal with this problem. On a preliminary version of the CS2007 Starter Site (that one with came with CS2007 Beta) they use class properties to deal with the appearance of the sample store page framework.

First step is create a new masterpage with a set of DIV files that can be enabled or disabled according with different page layout statuses. 

The second step is create a base class with a property to control the presentation

public abstract class SiteMasterPage : System.Web.UI.MasterPage
{
}

• Layout property: Control the appearance of the DIV areas inside the masterpage. Depending on context, the background code can change the layout of the page.

Now you need to create a new masterpage and insert code to deal with the properties:

• OnPreRender event: Deal with layout. Activating or deactivating DIVs according to the Layout properties

protected override void OnPreRender(EventArgs e)
{
 switch (this.LayoutStyle)
   {
  //se for mínimo, remove o banner, os sidebars, o menu e o rodapé.
            // deixa só o mainpagecontent
            case PageLayoutStyle.Minimal:
                this.adbanner.Visible = false;
                this.sidebar_a.Visible = false;
                this.sidebar_b.Visible = false;
                this.footer.Visible = false;
                this.menu.Visible = false;
                break;
  case PageLayoutStyle.Full:
      this.adbanner.Visible = true;
                this.sidebar_a.Visible = true;
                this.sidebar_b.Visible = true;
                this.footer.Visible = true;
                this.menu.Visible = true;
                break;
            }

            base.Page.Title = this.title; //ajusta o título da página
            base.OnPreRender(e); //executa a base do prerender
}

To get the design time class properties you can use a piece of code like the following one:

settings = PageSettings.GetPageSettings(HttpContext.Current.CurrentHandler.GetType());

internal static PageSettings GetPageSettings(Type type)
{
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    if (!type.IsSubclassOf(typeof(Page)))
    {
        throw new ArgumentException("SubClass", "type");
    }
    if (!PageSettings.typeCache.ContainsKey(type))
    {
        lock(typeCache)
        {
            // Não está em cache, temos que popular.
            if (!PageSettings.typeCache.ContainsKey(type))
            {
                string pageGroup = null;
                bool isSecure = false;
                PageLayoutStyle layoutStyle = PageLayoutStyle.Full;
                object[] attributes = type.GetCustomAttributes(true);
                foreach (Attribute attribute in attributes)
                {
                    if (typeof(PageLayoutAttribute).IsInstanceOfType(attribute))
                    {
                        layoutStyle = ((PageLayoutAttribute)attribute).LayoutStyle;
                    }
                }
                PageSettings.typeCache.Add(type, new PageSettings(layoutStyle, isSecure, pageGroup));
            }
        }

    }
    return PageSettings.typeCache[type];
}

At design time, we just need to tell how the page will look using the following approach:

[PageLayout(PageLayoutStyle.Full)]
public partial class _Default : System.Web.UI.Page
{

}

Remember that your ASPX page must be based on a MasterPage created from the custom base class.

public partial class StandardLayout : SiteMasterPage
{
...
}

Regards, Cris.
 

This posting is provided "AS IS" with no warranties, and confers no rights.

Posted: Tuesday, October 03, 2006 5:37 PM by crisag
Filed under:

Comments

No Comments

Anonymous comments are disabled
Page view tracker