Creating a Basic Custom Control

This topic describes how to create a very basic custom control using server-side code only, without any client-side interactivity.

This project on Github contains the sample project described in this topic. You can view the project on Github instead of writing out the code, if you prefer:
K2Documentation.Samples.Extensions.SmartForms.CustomControl

  1. Add a C# .cs code file to your custom control project (see Creating a Custom Control Project, for instructions on creating a custom control project)
  2. Add an embedded XML file called Definition.xml with the following content
    Note: The FullName node contains the namespace and class of the custom control .cs file, followed by the assembly name for your project.
    Copy

    Definition.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <ControlType>
      <FullName>CustomControl.BasicControl, CustomControl</FullName>
      <Category>Display</Category>
      <Group>Custom</Group>
      <Name>BasicControl</Name>
    </ControlType>

  3. Add the following code to the .cs code file. This code contains an attribute to indicate where the Control Type Definition XML file is located, and overrides the RenderContents() method to display "Basic Control" text using a HtmlTextwriter write function:
    Copy

    Add to your .cs code file

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    // SourceCode namespaces used in the control
    using SourceCode.Forms.Controls.Web.SDK;
    using SourceCode.Forms.Controls.Web.SDK.Attributes;

    namespace CustomControl.RenderContents
    {
        // This code contains an attribute to indicate where the Control Type Definition XML file is located,
        // and overrides the RenderContents() method to display "Basic Control" text using a HtmlTextwriter write function:

        [ControlTypeDefinition("[CustomControl.Definition.xml]")]
        class BasicControl : BaseControl
        {
            protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
            {
                writer.Write(" <b>Basic Control</b>");
            }
        }
    }
  4. Build the custom control project, and then go to the Deploying and Registering Custom Controls topic to learn how to deploy and register the custom control.