Return to doc.sitecore.com

Valid for Sitecore 5.3, 5.2
Creating Code Behind with VS2005

Creating code-behind is done differently in Visual Studio 2005 (compared to Visual Studio .NET).  The following steps describe the process.

  1. Create the desired layout or sublayout in Sitecore using the DeveloperCenter.
     
  2. Delete the layout (.html) or sublayout (.ascx) file generated by Sitecore in your file system.

    If you would like to add code-behind to an existing layout or sublayout, rename the existing file rather than deleting it.  In the next step you will create a new file.  Afterwards you should copy some of the contents from the original file to the new file.
  3. In Visual Studio, create a new item in the existing Sitecore project.
    Choose a new Web Form (for layouts) or a Web User control (for sublayouts).  Provide the exact same name as the layout or sublayout that you created in Sitecore.
     
  4. Make certain that the file was created in the same folder as the original Sitecore file.
     
  5. Add directives to the new file as follows:
     
    1. If you created a layout, add:

      <%@ OutputCache VaryByParam="none" Duration="100" %>
      <%@ register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>

      Add a directive for the code-behind class. Change the namespace and class reference, Viewstate.layouts.Main_Layout, to the namespace and class created above. (Change Viewstate to your current project name and the Main_Layout to the layout file name you created on step 1):

      <%@ Page language="c#" Inherits="Viewstate.layouts.Main_Layout" Codepage="65001" CodeFile="Main Layout.html.cs"%>
       

    2. If you created a sublayout, add:

      <%@ register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>

If you renamed an original file, copy the contents after the directives to the new file.

The layout or sublayout is now associted code-behind.