There are several different ways to add custom ASPX pages with code behind to SharePoint. The following link gives an overview of some of the most common ways to do this (http://sharenotes.wordpress.com/2008/02/21/add-custom-aspx-pages-or-asp-net-pages-in-sharepoint/) ; however, the below approach in this article is a new alternative which I find to be fairly quick, easy, and allows us to still utilize SharePoint’s security without needing to build and deploy a feature. This is a good approach if your pages are going to live in soley one place without the need to be reusable like webparts.
To make your custom ASPX page ready for Sharepoint, you will need to perform the following in the code behind ([filename.aspx].cs):
add all event handlers dynamically in the page load (we will also remove any event handler references in the aspx mark up)
Reference the Microsoft.SharePoint and Microsoft.SharePoint.Publishing assemblies. These are usually found at C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12ISAPI and C:Program FilesMicrosoft Office Servers12.0Bin
Implement Microsoft.SharePoint.Publishing.PublishingPageLayout instead of System.Web.UI.Page
public partial class _Default : Microsoft.SharePoint.Publishing.PublishingLayoutPage
protected void Page_Load(object sender, EventArgs e)
this.btnClear.Click += this.btnClear_Click;
this.btnSubmit.Click += this.btnSubmit_Click;
protected void btnSubmit_Click(object sender, EventArgs e)
int i = 0;
int i2 = 0;
if (txt1.Text.Length > 0)
i = Convert.ToInt16(txt1.Text);
if (txt2.Text.Length > 0)
i2 = Convert.ToInt16(txt2.Text);
txt3.Text = (i + i2).ToString();
catch (Exception ex)
txt3.Text = ex.Message + ex.StackTrace;
protected void btnClear_Click(object sender, EventArgs e)
txt1.Text = string.Empty;
txt2.Text = string.Empty;
txt3.Text = string.Empty;
In your aspx mark-up, make the following adjustment:
Remove all references to event handlers (e.g. onclick=”… )
%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”SharePointCustomPageTest._Default” %>
Build your project assembly (signing the assembly), adding it to your server’s gac (or web application bin), as well as registering the assembly as a SafeControl in your SharePoint web.config.
Increase business process efficiency and collaboration with Microsoft SharePoint
The final task is to get our aspx pages ready for SharePoint. Our aspx pages should basically follow the structure of page layouts (you can open and copy one of these using SharePoint Designer). Build your new aspx pages using the example structure shown below, placing our aspx code in the PlaceholderMain content placeholder, as well as adding reference to our assembly in the Inherits statement. I recommend this “Sharepoint-ready” aspx page be built external to your original project, as once the below changes are implemented to your page, your project will not build any longer.
<%@ Page Language=”C#” CodeBehind=”Default.aspx.cs” Inherits=”SharePointCustomPageTest._Default, SharePointCustomPageTest, Version=126.96.36.199, Culture=neutral, PublicKeyToken=acec0186600f0729″ %>
Once your page is ready, simply upload it into any SharePoint library (e.g. Pages library).