Return to doc.sitecore.com

Valid for Sitecore 5.2, 5.1.1
  Create a WebControl Rendering
Prev Next

There are two types of Web Controls: standard .NET Web Controls which inherit from System.Web.UI.WebControls.WebControl and generally override the method with signature protected void Render( HtmlTextWriter), and Sitecore Web Controls which inherit from Sitecore.Web.UI.WebControl and generally override protected void DoRender( HtmlTextWriter ).  The advantages and disadvantages of the various types of rendering components are listed in the concept resource on Renderings. 

First create the Web Control in Microsoft® Visual Studio .NET, inheriting from the appropriate base class and overriding the appropriate rendering method.  Ensure the assembly (DLL) containing the new Web Control exists in the /bin directory under the Sitecore site’s document root.

Because standard Web Controls cannot be bound to a Placeholder and currently Web Controls cannot be added to a Layout or Sublayout using drag-and-drop, it is not necessary to register them with Sitecore.  Edit the Layout or Sublayout which will contain the Web Control, register the tag prefix with a line such as the following:

<%@ register TagPrefix="custom" Namespace="custom" Assembly="custom" %>

(replace custom with your own TagPrefix, Namespace and Assembly (the name of the DLL file without the .dll extension).  The Web Control can then be invoked from the Layout or Sublayout with a line of code such as the following:

<custom:MyWebControl runat="server" />

(replace custom with your TagPrefix and MyWebControl with the class name of the Web Control).  Once the Layout or Sublayout has been edited in Visual Studio .NET or another text editing program it should no longer be edited with the browser-based WYSIWYG editor (Layout Editor).

The same approach can be used for Sitecore Web Controls unless they need to be bound to a Placeholder.  To bind Sitecore Web Controls to Placeholders, the control must be registered with Sitecore as a Rendering.  Unfortunately this wizards has not been completed:

  1. In content editor, change the first step of the treecrumb to Layout.
  2. Expand the Renderings node and select an existing Rendering.
  3. From the New menu select Add from Template.
  4. Select System, Layout, Renderings and Webcontrol.
  5. Move the new Rendering from under the existing Rendering to the main Renderings so that it is not a child of another Rendering.
  6. Complete the properties of the new Rendering. 
    1. Assembly: the name of the assembly containing the Web Control (the file name of the DLL without the .dll extension)
    2. Namespace: the namespace containing the class representing the Web Control
    3. Tag: the class name of the Web Control
    4. TagPrefix: the tag prefix to associate with the assembly.
  7. Associate the new Rendering with a Placeholder key for Templates and/or Items just as you would a Sublayout or XSL Rendering.

As a shortcut for registering additional Sitecore Web Control Renderings, duplicate an existing Web Control Rendering and change its properties (starting at step 6 above).


Prev Next