Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
 Adding Code-Behind Files to Master ...
Community Content
In this section
Statistics Annotations (5)
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.

Visual How To

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

MVP Icon  Andrew Connell, Critical Path Training, LLC

March 2008

Overview

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.

Code It

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.

C#
using System;
using System.Web.UI;

namespace MSDN.SharePoint.Samples {
  public class ContentPageTemplate : Page {
  }
}

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.

Read It

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.

See It

Adding Code-Behind Files to Master Pages

Watch the Video

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

Explore It
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Designer File      Mafi Osori   |   Edit   |   Show History
To access your page controls from your codebehind file, you could make a partial class file with all the controls declared.

Take a look at the page.designer.cs partial class file that Visual Studio automatically generates as your add and remove controls from your page in a regular ASP.NET project or website.

You can then create something similar for your SharePoint page.
Tags What's this?: Add a tag
Flag as ContentBug
What about the standard webpart controls on the page?      Roozbeh69   |   Edit   |   Show History
Very nice article, but the problem is when you replace Inherits attribute with your own, you are not able using standard webpart controls on your page any more, so I think you loose some capability like using attachments on your form and also the lack of styles and beauty. Is there any solution?

Many thanks,
Roozbeh
Tags What's this?: Add a tag
Flag as ContentBug
How-to video's links have suddenly disappeared!      aaroh_bits   |   Edit   |   Show History
I have viewed couple of vidoes on "How-to" series few months back but the videos links have been just removed recently.....
I guess others are facing some issue as well .....
--aaroh

None of the Sharepoint Services How-To videos appear to be working      Kilgore3000   |   Edit   |   Show History
There aren't any links to view the videos on any of the Visual How-to pages. Specifically, the "Watch the video" text should be a link, but it's not, it's just text. This used to work in the past.
Tags What's this?: Add a tag
Flag as ContentBug
something else you'll want to note...      jarretf ... Thomas Lee   |   Edit   |   Show History

When you need to reference a web control on your page, like a Label control, you also have to make reference to it in your class, else you will not have intellisense and you will get build errors when compiling a feature. Example:

public partial class migrateUser : LayoutsPageBase

{

protected Label lbl_myText;

protected override void OnLoad(EventArgs e)

{

lbl_myText.Text = "Hello";
...

Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker