Click here to Skip to main content
15,892,697 members
Articles / Web Development / CSS

Support for Generics in ASP.NET Server Controls

Rate me:
Please Sign up or sign in to vote.
4.77/5 (16 votes)
29 Jan 2009CPOL4 min read 61.1K   446   29  
Implementation of a framework to support Generics in ASP.NET server controls, including strong-typing of ITemplate containers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;

namespace CobaltSoftware.Foundation.Web
{
    /// <summary>
    ///     Various extension methods for use on web controls.
    /// </summary>
    public static class ControlExtensions
    {
        /// <summary>
        ///     Append a databound templated data container to the controls list of the current control.
        /// </summary>
        /// <typeparam name="TItemType">Type of item being displayed.</typeparam>
        /// <param name="control">Control to append to.</param>
        /// <param name="template">Template to instanciate into container.</param>
        /// <param name="item">Item value</param>
        /// <param name="bind">Call data-binding method on templated container?</param>
        /// <returns>Container that was added to child controls list.</returns>
        public static DataContainer<TItemType> AppendDataContainer<TItemType>(this Control control, ITemplate template, TItemType item, bool bind)
        {
            // No template? No work.
            if (template == null)
                return null;

            // Create the container
            DataContainer<TItemType> container = new DataContainer<TItemType>() { DataItem = item };
            template.InstantiateIn(container);
            control.Controls.Add(container);

            if (bind)
                container.DataBind();

            return container;
        }
        
        /// <summary>
        ///     Append a databound templated indexed data container to the controls list of the current control.
        /// </summary>
        /// <typeparam name="TCollectionType">Collection type</typeparam>
        /// <typeparam name="TItemType">Type of item being displayed.</typeparam>
        /// <param name="control">Control to append to.</param>
        /// <param name="template">Template to instanciate into container.</param>
        /// <param name="collection">Collection item belongs to</param>
        /// <param name="item">Item value</param>
        /// <param name="bind">Call data-binding method on templated container?</param>
        /// <param name="index">Item display index</param>
        /// <returns>Container that was added to child controls list.</returns>
        public static IndexedDataContainer<TCollectionType, TItemType> AppendIndexedDataContainer<TCollectionType, TItemType>(this Control control, ITemplate template, TCollectionType collection, TItemType item, bool bind, int index)
        {
            // No template? No work.
            if (template == null)
                return null;

            // Create the container
            IndexedDataContainer<TCollectionType, TItemType> container = new IndexedDataContainer<TCollectionType, TItemType>() { DataItem = item, Index = index, Collection = collection };
            template.InstantiateIn(container);
            control.Controls.Add(container);

            if (bind)
                container.DataBind();

            return container;
        }

        /// <summary>
        ///     Write some information to the current trace.
        /// </summary>
        /// <param name="control">Control</param>
        /// <param name="message">Message to write</param>
        public static void TraceInformation(this Control control, String message)
        {
            if (HttpContext.Current.Trace != null)
                HttpContext.Current.Trace.Write(control.UniqueID, message);
        }

        /// <summary>
        ///     Write some warning information to the current trace.
        /// </summary>
        /// <param name="control">Control</param>
        /// <param name="message">Message to warn with</param>
        public static void TraceWarning(this Control control, String message)
        {
            if (HttpContext.Current.Trace != null)
                HttpContext.Current.Trace.Warn(control.UniqueID, message);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Insurance Industry
United Kingdom United Kingdom
Steve Gray is a Senior Developer at a British insurance company, working on a popular aggregator. When he's not writing ASP .NET, it's because there's SQL or WCF to write instead.

Comments and Discussions