Introduction
While wrapping a GenericContent control in a custom control has its advantages, it is quicker to do it with a user control. This article is available in the
offline help as well, but we decided to make it available as a KB, as well.
Extensibility
Extending an existing control provided in Sitefinity is useful and time-saving - customizing existing controls take less time than creating new ones from scratch. The functionality the controls offer is prone to extension but it is up to each customer to define the exact requirements. This topic will provide an example for extending controls in Sitefinity by adding the Generic Content control to a user control.
Entering content is the main functionality of the Generic Content control. Still, additional options such as choosing what to be visible to whom or when it should be visible are very useful. This would come in handy when setting different content of a page for registered and for unregistered users.
Example
A user control could wrap an existing GenericContent control, and implement the custom logic. The user control should implement IGenericContent interface and its Content property should have an appropriate ControlDesignerAttribute. For example:
Mark-up:
| <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WrappingGCInUserControl.ascx.cs" Inherits="Tickets_WrappingGCInUserControl" %> |
| <%@ Register Assembly="Telerik.Cms.Engine" Namespace="Telerik.Cms.Engine.WebControls" TagPrefix="sfWeb" %> |
| <dl> |
| <dd> |
| <h2>Title</h2> |
| </dd> |
| <dd> |
| <sfWeb:GenericContent runat="server" ID="GenericContent1" /> |
| </dd> |
| <dd> |
| Footer |
| </dd> |
| </dl> |
Code-behind:
| using System; |
| using System.ComponentModel; |
| using System.Web.UI; |
| using Telerik.Cms.Engine; |
| using Telerik.Cms.Engine.WebControls; |
| using Telerik.Framework.Web.Design; |
| using Telerik.Web; |
| [ControlDesignerAttribute("Telerik.Cms.Engine.WebControls.Design.GenericContentDesigner, Telerik.Cms.Engine")] |
| public partial class Tickets_WrappingGCInUserControl : System.Web.UI.UserControl, IGenericContent |
| { |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| } |
| #region IGenericContent Members |
| [CmsBrowsable(false)] |
| [CmsPersistable(true)] |
| public string Content |
| { |
| get |
| { |
| return GenericContent1.Content; |
| } |
| set |
| { |
| GenericContent1.Content = value; |
| } |
| } |
| [Browsable(false)] |
| public Guid ContentID |
| { |
| get |
| { |
| return GenericContent1.ContentID; |
| } |
| set |
| { |
| GenericContent1.ContentID = value; |
| } |
| } |
| public string ProviderName |
| { |
| get |
| { |
| return GenericContent1.ProviderName; |
| } |
| set |
| { |
| GenericContent1.ProviderName = value; |
| } |
| } |
| [Browsable(false)] |
| public IContent SharedContent |
| { |
| get |
| { |
| return this.GenericContent1.SharedContent; |
| } |
| } |
| public bool UseStagedVersion |
| { |
| get |
| { |
| return GenericContent1.UseStagedVersion; |
| } |
| set |
| { |
| GenericContent1.UseStagedVersion = value; |
| } |
| } |
| [CmsBrowsable(false), CmsPersistable(true)] |
| public bool HasDynamicLinks |
| { |
| get |
| { |
| return GenericContent1.HasDynamicLinks; |
| } |
| set |
| { |
| GenericContent1.HasDynamicLinks = value; |
| } |
| } |
| public string GetItemUrl(string provider, Guid id, bool resolveAsAbsoluteUrl) |
| { |
| return GenericContent1.GetItemUrl(provider, id, resolveAsAbsoluteUrl); |
| } |
| public void RenderBinaryContent(HtmlTextWriter writer) |
| { |
| GenericContent1.RenderBinaryContent(writer); |
| } |
| public void RenderTextContent(HtmlTextWriter writer) |
| { |
| GenericContent1.RenderTextContent(writer); |
| } |
| #endregion |
| } |