Creating the custom control

In this tutorial you will create a control that consists of two parts -- a custom control and a Silverlight application. This topic focuses on the creation of the custom control.

The topic explains the following:

Creating a custom control

The control can reside in Sitefinity solution or in another solution. You will create a control in the scope of the Sitefinity's solution.

To create an empty custom control, perform the following:

  1. From the context menu of Sitefinity's solution, click Add » New project...
  2. From the dialog choose Visual C# » Web » ASP.NET Server Control.

  3. Name the project BookWidget and click OK. A new project is generated under the solution. It contains only a single .cs file namedServerControl1.

  4. Rename the file to Book.cs. You must also rename the class inside the file to Book.
  5. Open the file and remove any auto-generated code besides the class definition.

    The code inside your Book.cs file must look like this:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    namespace BookWidget
    {
        public class Book : WebControl
        {
     
        }
    }

Inheriting the SimpleScriptView class

In Sitefinity, you can create your controls as standard ASP.NET custom controls or you can derive them from one of the built-in base classes - SimpleViewSimpleScriptView. In this tutorial you derive the Book control from the SimpleScriptView class. To do this, you must add a reference to the Telerik.Sitefinity.dll in your BookWidget project. You do this in the following way:

  1. Form the context menu of project BookWidget, click Add Reference...
  2. In the dialog select the Browse tab.
  3. The Telerik.Sitefinity assembly is located in folder SDK\Content\Common\Dependencies inside Sitefinity installation folder. Select it and click OK.
  4. Open the Add Reference dialog again.
  5. Add a reference to the System.Web.Extensions assembly, which is located in the .NET tab.

  6. Open the Book.cs and import the using Telerik.Sitefinity.Web.UI; statement.

  7. Modify the class definition to the following:

    public class Book : SimpleScriptView
    {
        public override IEnumerable<System.Web.UI.ScriptDescriptor> GetScriptDescriptors()
        {
            throw new NotImplementedException();
        }
     
        public override IEnumerable<System.Web.UI.ScriptReference> GetScriptReferences()
        {
            throw new NotImplementedException();
        }
     
        protected override void InitializeControls(GenericContainer container)
        {
     
        }
     
        protected override string LayoutTemplateName
        {
            get
            {
                throw new NotImplementedException();
            }
        }
    }

To inherit the class you must implement the GetScriptDescriptors, the GetScriptReferences, the InitializeControls and theLayoutTemplateName abstract members.

Creating a template

Custom controls are usually represented by a class, which implements their functionality. The mechanism of creating controls in Sitefinity allows you to specify a template. With a template you can specify the visual representation of your control. A template is represented by an.ascx file. In the Book control the template hosts the Silverlight object. To create a template for it, perform the following:

  1. In the BookWidget project, add a new folder named Resources.
  2. In it create a folder named Views.
  3. From the context menu of folder Views, click Add » New Item...
  4. From the dialog click Visual C# » Code » Code File.

  5. Name the file Book.ascx and click Add.

    An empty .ascx file is generated under the Views folder.

  6. From the context menu of file Book.ascx, select Properties.
  7. In the Properties pane, set the Build Action property of the file to Embedded Resource.

Referencing the template

To specify a default template for the control, you must override the LayoutTemplateName property in the Book class. It must return a string that represents a reference to the embedded resource, which represents your template. To reference it, use the following code:

protected override string LayoutTemplateName
{
    get
    {
        return "BookWidget.Resources.Views.Book.ascx";
    }
}

The LayoutTemplateName follows the naming convention <Namespace>.<ResourceFolderPath>.<TemplateName>, where you must replace path slashes inside the resource folder path with dots.

In addition to the LayoutTemplateName, the SimpleScriptView exposes the LayoutTemplatePath property. It is used to refer an external template that is placed outside of the assembly.

Referencing the control

You must reference the assembly of the control in the Sitefinity project. In this tutorial the control and Sitefinity project are located in one and the same solution. To add a reference, perform the following:

  1. From the context menu of Sitefinity project, click Add Reference...
  2. Click Projects tab.
  3. Select the BookWidget project and click OK.

    The reference is added and you can register and use the control in your Sitefinity application.

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK