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)
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
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)
{
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/>"
;
}
}
}
View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.