Try Now
More in this section
Blogs RSS feed

Running Server-Side Code in Sitefinity Widget Templates

by Josh Morales

Breaking Changes

NOTE: As of Sitefinity 4.2, new security enhancements will automatically strip server side code from widget templates. If you wish to run server side code on your widget templates, see this article on Mapping External Templates for Sitefinity 4 Widgets.


Widget templates in Sitefinity 4 provide allow you customize the layout right inside the Sitefinity Administration, but did you know that you can also use them to execute server-side code? Today we’ll take a quick look how this is done, and how you can use it to bring even more power to your widgets.

Script Tag

If you look at a widget template, you’ll notice that the markup looks a lot like a standard user control. That’s because, for all intents and purposes, widget templates are loaded internally by Sitefinity as User Controls when bound to the widget.


This means you can do pretty much anything that you can do in a standard user control, including running server-side code. However, because the widget templates don’t use the code-behind model, we have to take advantage of the script tag, decorating it with the runat=”server” attribute so that it executes on the server.

By adding this tag to the widget template, we can now run any server side code, including accessing page life-cycle events like Init, Load, etc.

<script runat="server"> protected void Page_Load(object sender, EventArgs e)


So what can you do with this new opportunity? As an example, I'll show you how you can easily modify the Details View of the Events View Widget to update the Page Title to match the name of the selected event using the Sitefinity Non-Profit Starter Kit.

Here is the code to add to the widget details view template, after which we'll take a closer look at what is happening.

<script runat="server"> protected void Page_Load(object sender, EventArgs e)
        var view = this.Parent.Parent as Telerik.Sitefinity.Modules.Events.Web.UI.Public.DetailsView;
        if (view == null) return;

        var detailItem = view.Host.DetailItem;
        if (detailItem == null) return;

        Page.Title = detailItem.Title;

The widget template is hosted inside a GenericContainer which itself then is inside the actual DetailsView object. We gain access to the view by casting the control's parent's parent to the DetailsView.

Next we grab the DetailItem from the Host property of the DetailView. This gives you immediate and convenient access to the item referenced on the details page.

Finally we simply update the Page Title to match the Title property of the item. It's really that simple!


What's Next

Although this is a powerful way to take control of your templates, it is admittedly somewhat cumbersome to work with server-side code inside the template editor. The biggest missing element is the convenience of Intellisense in Visual Studio.

Fortunately, it is possible to edit templates in Visual Studio by mapping your widget template to an external file. This is the subject of our next post.

Until then try it out, and be sure to share your experiences, questions, and comments in our Sitefinity 4 discussion forum.


Leave a comment
  1. Chris Smith Sep 06, 2011
     Hey Josh,
    Hmmmm....Not so sure about this- whenever I add a <script> or <script runat="server"> tag to a widget template and save it Sitefinity (4.2, Win 7 Pro x64) strips out the tag for me automatically. Nothing is left. 
    <asp>, <% ... %>, <%@ ... %>, tags do nothing but echo out the code I am trying to run.
    Would you mind clarifying exactly what you claim your software can do please?
  2. Chris Smith Sep 07, 2011
    For those who stumble across this post, apparently there is a security enhancement with Sitefinity 4.2 (and presumably future releases) which now prohibits inline server side code being inserted within the CMS (hence your script tags will be stripped).
    For those who have the same problem, the key is to copy the template into a self-created ascx control, which can override the default widget template. 

    When you have created the control, theoretically you should be able to specify a TemplatePath (from the widget, Edit > ControlDefinition > Views > * > TemplatePath) to point at your new ascx control, which can contain server side code without issue. This requires deleting the TemplateKey (same place) definition (a GUID associating widget and template), which takes precedent within Sitefinity. 

  3. Josh Sep 07, 2011
    Indeed you are correct, I will update this post shortly to reflect this change.

    As an alternative to mapping the individual controls, you can also change them globally with the ViewMap. For more information on that see this article: Customizing Sitefinity 4 Controls with the ViewMap

    Thank you for taking the time to share your feedback!
  4. eb-1988 Mar 22, 2012
    I've found a hack ;) 

    <script type="C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)

    I've tested this with SF 5

    Leave a comment