Some couple of points we need to take care regarding implementing behind code in ASP.NET are the following:-
• The first and foremost requirement is that behind code should be registered in to the GAC assembly. In other words we need to code the behind code in a separate assembly / namespace and then compile the same in a DLL. Later we need to register the DLL in GAC.
• Second we need to use the assembly directive to refer the behind code.
Step 1:- So the first step is to make two solution files one is the behind code which goes in separate assembly ‘ClassLibrary1’ namespace and the other is the ASP.NET web project which has the ‘SimplePageCodeBehind.aspx’. We also need to register this DLL in a GAC. So you need to generate a strong name give to the assembly and register the same using the ‘GACUTIL’ provided by the .NET framework.
Step 2:- The behind code is in a separate assembly as need to register the same in the GAC. We have kept the behind code simple. We have create two label objects and set the value. One of the important points to be noted is that we have referenced the ‘System.Web.UI’ namespace DLL and ‘Microsoft.SharePoint’ namespace DLL. The other point to be noted is that the class inherits from ‘LayoutsPageBase’ class which belongs to ‘Microsoft.SharePoint’ namespace.
// need to refer the UI objects of ASP.NET
// Need to refer the SharePoint DLL
// Inherit the behind code from ‘LayoutsPageBase’ class of SharePoint
public partial class _Default : LayoutsPageBase
protected Label lblSiteQuestion;
protected Label lblSiteAnswer;
protected override void OnLoad(EventArgs e)
lblSiteQuestion.Text = " How can we implement behind code in SharePoint ?";
lblSiteAnswer.Text = " We need to register the behind DLL in GAC";
We need to also register the above DLL in GAC. So we need to generate a strong name and register the same using GACUTIL.
Step 3:- Now comes the UI part of the ASP.NET i.e. the ASPX page. Below is the code snippet which shows how the ASP.NET UI looks like.
The first thing to note is that behind code is not referred as code behind but is referred using the GAC public token key. In order to refer it using GAC key we need to use the ‘Assembly’ attribute for the same.
We have also inherited from the master page file i.e. ‘Application.Master’ so that we have a consistent look and feel.
<!—Refer the sharepoint assembly ->
<%@ Assembly Name="Microsoft.SharePoint,Version=188.8.131.52, Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<!—Refer the behind code, Note that the behind code is coded in a different assembly and registered in the GAC ->
<%@ Assembly Name="ClassLibrary1, Version=184.108.40.206, Culture=neutral,PublicKeyToken=af6d081bf267e17e" %>
<!— In order to maintain consistent look and feel we need to inherit from the Application.Master page ->
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="ClassLibrary1._Default" EnableViewState="false" EnableViewStateMac="false" %>
Once we have referred the Assembly and set the Page attributes. Its time to fill the content in the placeholders defined in the master page ‘Application.Master’.
<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">
<table border="1" cellpadding="4" cellspacing="0" style="font-size:12">
<td><b><asp:Label ID="lblSiteQuestion" runat="server" /></b></td>
<td><asp:Label ID="lblSiteAnswer" runat="server" /></td>
<asp:Content ID="PageTitle" runat="server"
SharePoint Behind code implementation
<asp:Content ID="PageTitleInTitleArea" runat="server"
When we want to implement behind code we need to register the same in GAC.
Note: - Do not try to compile the project in VS.NET IDE. You can only compile the class assembly. The ASPX file you need to later paste it to the ‘_layout’ directory i.e. ‘C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS’.
Once you have copied the ASPX file and registered the behind code assembly in GAC, you can run and enjoy how it looks like.
If you are thinking that behind code pages implementation is lot of pain in SharePoint. Hang on as we move ahead you will see better way of implementation. Second we can not rule out the benefits as compared to the pain incurred in implementing behind code pages in ASP.NET.
Asked In: Many Interviews |