Share via


Adding Code-Behind Files to Master Pages and Content Pages in Windows SharePoint Services 3.0

Summary:  Learn how to use code-behind files in custom content pages in Windows SharePoint Services 3.0.

Applies to:  Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005

  Andrew Connell, Critical Path Training, LLC

March 2008

Many things that ASP.NET 2.0 developers usually build seem impossible or challenging in a Windows SharePoint Services environment. However with the latest version, Windows SharePoint Services 3.0 architecture is based on ASP.NET 2.0 and therefore enables developers to use virtually all the same techniques that are implemented in ASP.NET 2.0 sites in SharePoint sites. This Microsoft Office Visual How To demonstrates one of these techniques, using code-behind files within custom content pages.

First, create a new class file (this example uses Microsoft Visual C#) in a Class Library project that inherits from the System.Web.UI.Page class (or System.Web.UI.MasterPage if you are creating a code-behind file for a master page). The name of this class should be the same name as the file, but it is not required.

Wiring the Code-Behind File to the Master Page or Content Page

The next step is to wire the code-behind file to the master page or content page. You do this by entering the fully qualified name, otherwise known as the five-part name, in the Inherits attribute of the page or master page directive. The five-part name is the full type name (namespace.class) followed by the strong name of the signed assembly.

<%@ Page MasterPageFile="~masterurl/default.master" 
meta:progid="SharePoint.WebPartPages.Document" 
Language="C#" Inherits="MSDN.SharePoint.Samples.ContentpageTemplate, 
MSDN.SharePoint.Samples.SharePointPagesWithCodeBehind, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=ae015afe5f30fb68" %>
<%@ Master language="C#" Inherits="MSDN.SharePoint.Samples.MinimalMasterTemplate, 
MSDN.SharePoint.Samples.SharePointPagesWithCodeBehind, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=ae015afe5f30fb68" %>

At this point the code-behind file is wired up with the content page or master page, and developers can do everything they are used to doing in ASP.NET 2.0 development, such as overriding the OnLoad method or binding data to controls on the page.

Adding code-behind files in ASPX or ASCX files within a traditional ASP.NET 2.0 environment is very easy thanks to Microsoft Visual Studio. All you have to do is right-click the file in Visual Studio, either in Solution Explorer or in the designer, and select View Code. Visual Studio then creates a new code-behind file (if one did not already exist), adds the minimal code for the new class to inherit from the System.Web.UI.Page class, and then adds the necessary information to the Inherits attribute in the Page or Control directive in the ASPX or ASCX file. This tells the Microsoft .NET Framework where the associated class for the file is located. Unfortunately Visual Studio cannot do the same things for custom SharePoint Server content pages. Instead, you must do these things manually. The same is true for master pages in SharePoint sites.

One word of caution: do not add inline code to the page. Put all code in a code-behind file. Why? Because when a page is not customized, or when the source of the page is still pulled from the file on the file system instead of the database, SharePoint Server allows the page to run normally. However, if you customize the page by using a tool such as Microsoft Office SharePoint Designer 2007, the page is run through the safe mode parser in SharePoint Server, which blocks any inline code from running.

Watch the Video

Length: 14:13 | Size: 14.1 MB | Type: WMV