Try Now
More in this section
Blogs RSS feed

Sitefinity and the ASP.NET Code-Behind Model

by Peter Marinov

There are many ways to programmatically modify the pages which Sitefinity dynamically generates. One way is to develop .ascx user controls and plug them into the pages as Layout controls, Page controls or Form controls. The process is pretty straight forward and there are many resources devoted to this very topic.

Another way to provide a code-behind for the pages that Sitefinity renders in response to clients’ requests is to use the built-in functionality of the Code behind type filed. The field is inserting a new layer of usability into Sitefinity and that is what I want to focus your attention on.

Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your business logic. Since the markup that Sitefinity renders is dynamically generated via the engine of the CMS application, there is no a physical .aspx file to be found in the database or in the file system of the project.

However, Sitefinity allows you to provide a code–behind file that will be included into the precompilation associated with each page, thus enabling you to add your own custom logic via the classic ASP.NET Web Forms code-behind model.

And here is where the Code behind type field comes in handy - it connects the dynamically generated page with a physical code-behind file (see the picture below)


Here are the few steps you need to follow:

Step 1. Go to Visual Studio and create a new class file within your Sitefinity project. In my case this will be a C# file. 

Step 2. The next thing that shall be done is to inherit from the System.Web.UI.Page. In my case I have created a class that is called CodeBehind


System.Web.UI.Page is the class that represents the .aspx file, also known as a Web Forms page. So, the classes that inherit from those System.Web.UI.Page are eligible to provide the presentation logic in the code-behind method for ASP.NET pages.


Step 3. Implement your own logic by accessing all the properties and controls on the page or you can bind your own event handlers to the events, which the ASP .NET page or controls raise. Have in mind that since there is no an associated designer class, you will have to first identify the controls by code and then manipulate them.

Step 4. Remember to leave the build action to “Compile” in all of the cases that your class file is located into your Sitefinity application folder. This way your class will be precompiled into an working assembly, that can be then accessed by Sitefinity and the ASP .NET. The latter will include your code-behind code into final assembly that runs to render the output to the browser.

Step 5. Build your Sitefinity application so to compile your class into an assembly.

Step 6. Go in the backend of Sitefinity and click on the Title and Properties of the selected page for which you want to add the code.

Step 7. Register the class with its full qualified name following the convention Namespace.Page, Assembly. For that purpose you can use the help of the object explorer tool in Visual Studio. (see the picture)


I have provided a simple code snippet that renders some page’s controls data into the browser by using the Sitefinity API. Note that this code will render only the controls that are placed on the page. Those that were part of the template will not be included into the list of child controls for the page that is based on the template:


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Web;
using Telerik.Sitefinity.Pages.Model;
namespace SitefinityWebApp
    public partial class CodeBehind : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            Button button = new Button();
            button.Text = "Click to show the controls";
            PageManager pManager = PageManager.GetManager();
            PageData page = pManager.GetPageNode(new Guid(SiteMapBase.GetCurrentProvider().CurrentNode.Key)).Page;
            foreach (PageControl control in page.Controls)
        private string getControlInfo(PageControl control)
            return "<Control Type: >" + control.GetType().ToString() + "<Now the object type: >" + control.ObjectType + "<Now the PlaceHolder’s name:>" + control.PlaceHolder + "<br/>";


Leave a comment
  1. Euclid Public Library Jan 03, 2012
    Would this method work with the Backend Pages?  Specifically, would this work for backend pages that contain the UI for the built-in modules?
  2. Georgi Jan 05, 2012
    This method will work for backend pages. In fact, there's no real difference between backend and frontend pages - they are just attached to different Sitemap nodes. 
  3. Viktor Jan 11, 2012
    Works well with our requirements. Thank you!
  4. Peter Mariov Jan 20, 2012
    FYI: There is a public string property CodeBehindType, a member of the PageData class (Telerik.Sitefinity.Pages.Model.PageData). Using the API of Sitefinity, you can programmatically set this property to manage the code-behind for a page/selection of pages.

    Leave a comment