Skip to content

Adding a BaseMaster extension class to MasterPage for SiteMap configurations

June 27, 2012

The master page sounds like a good place to put logic and code that will tell the page how to do something. As I think Masterpage is meant to control all pages under it? Thats what I believe, so here was an attempt and solution to solve the problem of having my ASP.NET website have a sitemap configuration for the menu placed in the well MasterPage.

Initial Problem – I wanted to hardcode the Response.Write(“content”) on the MasterPage codebehind to write anything in the code base of BaseMaster straight to the MasterPage page on the web. Turns out it doesn’t work at all, as its not visible in the MasterPage.

Here is a base class that inherits the MasterPage which I can use to store the variables for things related to configuration. The page can use any master page that inherits from which we call BaseMasterPage. You need an @ MasterType directive set to the base class.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PortalConfiguration;

/// <summary>
/// Summary description for BaseMaster
/// </summary>
public class BaseMaster : System.Web.UI.MasterPage
{
public virtual string Title
{
get
{
return “Hey!!!!!!!!!!!!!!!!!!!”;
}
set
{

}
}
// Code which gives error
public virtual void LoadTitle()
{
Response.Write(Title);
}

public virtual void Configuration(string configSection)
{
PortalConfiguration.CheckConfiguration(configSection);
}
}

This threw the obvious of 

Object reference not set to an instance of an object.

Line 35: </style>--%>
Line 36: 
Line 37: <title><% Master.LoadTitle(); %></title> Line 38: </head>
Line 39: <body>

This was my attempted “try to be clever” quickfix, which didn’t work!! I then had to find a way for the MasterPage to interact with configuration of the method Title to be displayed on the MasterPage, so I used <title><asp:Label runat=”server” ID=”HeaderLabel” /></title> and set HeaderLabel.Text to Master.Title which logically seemed right. Wrong this gave an error!

Line 21:     {
Line 22:         this.UserName.Text = ((PortalIdentity)((PortalPrincipal)Context.User).Identity).User.Username;
Line 23: HeaderLabel.Text = Master.Title; Line 24:     }
Line 25: 

After pondering for a while it seems you had to create an instance of BaseMaster for the MasterPage and ASP.NET application to see, I don’t understand as to why but you do need to do this! Like so: – 

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Principal;
using System.Web.Security;

public partial class Management : System.Web.UI.MasterPage
{
BaseMaster baseMaster = new BaseMaster();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.UserName.Text = ((PortalIdentity)((PortalPrincipal)Context.User).Identity).User.Username;

HeaderLabel.Text = baseMaster.Title;
SiteMapLabel.Text = baseMaster.Configuration();
}
}

protected void btnLogOut_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}

}

Notice I have added !IsPostBack to stop writing the SiteMap more than once, I have also wrote a static variable for the SiteMap string which sets to “” in case a postback does occue to which that string is fixed to the component: – 

HeaderLabel.Text

This solved the problem! So my sitemap gets stored onto the Header.Label text but on more thing I found out is that using Label embeds a <span> tag inside which is not what we want, we therefore use <title><asp:Literal runat=”server” ID=”HeaderLabel” /></title> in the Masterpage file and we see that it now works and displays Hey!!!! in the title of the ASP.NET web application.Image Image

There you have it a lot that I have covered and also discovered in ASP.NET today, more discussions surrounding MasterPage and SiteMap configurations to explain about this in further posts.

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: