Sitefinity and the ASP.NET Code-Behind Model

Sitefinity and the ASP.NET Code-Behind Model

December 27, 2011 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

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)

image

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)

image

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)
            {
                this.Page.Response.Write(getControlInfo(control));
            }
        }
 
        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/>";
        }
    }
}
 
progress-logo

The Progress Guys

View all posts from The Progress Guys on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments
Comments are disabled in preview mode.
Topics
 
 
Latest Stories in
Your Inbox
Subscribe
More From Progress
d12fcc0bdb669b804e7f71198c9619a7
5 Questions Automakers Should Ask to Improve Asset Uptime
Download Whitepaper
 
SF_MQ_WCM
2018 Gartner Magic Quadrant Web Content Management (WCM)
Download Whitepaper
 
What-Serverless-Means-For-Enterprice-Apps-Kinvey
What Serverless Means for Enterprise Apps
Watch Webinar