Custom Control using ControlBuilder

In this example we are going to create a custom control which has child elements, we will use the ControlBuilder method GetChildControlType to return the correct Type, so they can be rendered correctly.

The control we are going to build is a DownloadList control which have child Download items.


        
            
            
        

Let’s start be defining our Download class


namespace ControlBuilderTestApp
{
    public class Download
    {
        public string DownloadId { get; set; }
        public string Text { get; set; }
        public string FilePath { get; set; }
    }
}

Next let’s create our DownloadList Control


namespace ControlBuilderTestApp
{
    [ControlBuilderAttribute(typeof (DownloadListControlBuilder))]
    public class DownloadList : Control, INamingContainer
    {
        private readonly Hashtable _downloads = new Hashtable();
        private HtmlGenericControl _division;
        public string Title { get; set; }

        protected override void AddParsedSubObject(object obj)
        {
            Download download = obj as Download;

            if (download != null)
            {
                _downloads.Add(download.DownloadId, download);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (_downloads.Count == 0)
                return;

            HtmlGenericControl listItem;
            HtmlGenericControl link;
            HtmlGenericControl list = new HtmlGenericControl("ul");

            foreach (Download download in _downloads.Values)
            {
                link = new HtmlGenericControl("a");
                link.Attributes.Add("href", download.FilePath);
                link.Controls.Add(new LiteralControl(download.Text));


                listItem = new HtmlGenericControl("li");
                listItem.Controls.Add(link);

                list.Controls.Add(listItem);
            }

            _division.Controls.Add(list);
        }

        protected override void CreateChildControls()
        {
            LiteralControl header = new LiteralControl(string.Format("{0}", Title, "1"));

            _division = new HtmlGenericControl("div");
            _division.Controls.Add(header);

            base.Controls.Add(_division);

            base.CreateChildControls();
        }
    }
}

The CreateChildControls method creates our DIV tag for the title and content to be placed into.

The AddParsedSubObject method processes each of the child objects in the control declaration, and adds items to a hashtable to use later.

The OnPreRender method them produces a UL and LI list of all the downloads in the hashtable.

The key line to notice is the ControlBuilderAttribute declaration, this references the ControlBuilder


namespace ControlBuilderTestApp
{
    public class DownloadListControlBuilder : ControlBuilder
    {
        public override Type GetChildControlType(string tagName, IDictionary attribs)
        {
            if (String.Compare(tagName, "download", true) == 0)
            {
                return typeof (Download);
            }
            return null;
        }

        public override void AppendLiteralString(string s)
        {
            //base.AppendLiteralString(s);
        }
    }
}

The ControlBuilder checks each of the child controls to determine and return the Type based on the tag name.


My Downloads

Above is the output.

You can download the Custom Control using ControlBuilder project here



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.