WebControl with XML Parameters

by Moridin8 1. May 2007 19:51

For 'spaz99'

Sometimes as you may already know, custom controls can become somewhat large. With many features and abilities that need to be configured as part of their usual operation, usually this is done via the use of custom attributes. However this does get unwieldy on the larger objects.

M$ not being altogether immune from practicality catered for this with ASP.NET by providing the [PersistenceMode(PersistenceMode.InnerProperty)] attribute.  This allows us to move sizeable attributes to inner XML based elements instead, while being able to retain the properties in our objects.

An example of which is given below.

namespace InnerPropControlExample
{
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;
       
    [ToolboxData(@"<{0}:InnerPropControl runat=""server"">        
    <Parameters>
        <Param Name=""param1"" Value=""value1"" />
        <Param Name=""param2"" Value=""value2"" />
        <Param Name=""param3"" Value=""value3"" />
    </Parameters>
</{0}:InnerPropControl>")]
    public class InnerPropControl : WebControl
    {
        /// <summary>
        /// Holds the parameters during object lifetime
        /// </summary>
        private ParamCollection _par;

        /// <summary>
        /// Gets or sets the parameters.
        /// </summary>
        /// <value>The parameters.</value>
        /// MRW: 21/02/2007 
        [Browsable(false)]
        public ParamCollection Parameters
        {
            set { this._par = value; }
            get { return this._par; }
        }

        /// <summary>
        /// Renders the control to the specified HTML writer.
        ///   -- In this case it simply renders out the name/value parameers
        /// </summary>
        /// <param name="writer">The HtmlTextWriter that receives the content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            foreach( Param par in this._par )
                writer.Write("{0} = {1}<br/>\n", par.Name, par.Value);

            base.Render(writer);
        }
    }

    /// <summary>
    /// A basic Name/Value container
    /// </summary>
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public class Param
    {
        public string Name;
        public string Value;
    }

    /// <summary>
    /// A collection for the Name/Value container
    /// </summary>
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public class ParamCollection : IList<Param>
    {
        private List<Param> _col = new List<Param>();

        /// <summary>
        /// Sets the parameters as they are added by ASP.NET
        /// </summary>
        /// <value>The param.</value>
        public Param Param
        {
            set { this._col.Add(value); } // notice the Add!
        }

        // Everything below is pretty much standard...

        Param IList<Param>.this[int index]
        {
            get { return this._col[index]; }
            set { this._col[index] = value; }
        }
        int IList<Param>.IndexOf(Param item)
        {
            return this._col.IndexOf(item);
        }
        void IList<Param>.Insert(int index, Param item)
        {
            this._col.Insert(index, item);
        }
        void IList<Param>.RemoveAt(int index)
        {
            this._col.RemoveAt(index);
        }
        void ICollection<Param>.Add(Param item)
        {
            this._col.Add(item);
        }
        void ICollection<Param>.Clear()
        {
            this._col.Clear();
        }
        bool ICollection<Param>.Contains(Param item)
        {
            return this._col.Contains(item);
        }
        void ICollection<Param>.CopyTo(Param[] array, int arrayIndex)
        {
            this._col.CopyTo(array, arrayIndex);
        }
        int ICollection<Param>.Count
        {
            get { return this._col.Count; }
        }
        bool ICollection<Param>.IsReadOnly
        {
            get { return false; }
        }
        bool ICollection<Param>.Remove(Param item)
        {
            return this._col.Remove(item);
        }
        public IEnumerator<Param> GetEnumerator()
        {
            return this._col.GetEnumerator();
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this._col.GetEnumerator();
        }
    }
}

 

Tags: , , , , ,

Articles

Powered by BlogEngine.NET 1.5.0.7

About Matt R.Warren

MeMy name is Matt and I am the current tenant of this small corner of the internet. I mostly architect, design and prototype applications that use .NET with C# and a little C++/CLI for Enterprise although I am aware of and enjoy fully embracing Java based solutions and alternatives such as Mono/Linux.  

I have worked on projects ranging from small tools to large distributed real-time Enterprise systems ranging from EPOS and real-time/JIT stock management systems, to distributed applications for National/International Utility, Healthcare, Insurance and Finance  in the private sector in both the USA and the EU.

My LinkedIn Profile (Opens new window/tab)

“Matt is one of the brightest people I've worked with. His in-depth knowledge of the .NET frameworks has been a tremendous benefit to nVISIA and our clients. His knowledge of software architecture in general allows him to architect systems for the best fit to his client's needs.” 
Dan Christopherson , Technical Director , nVISIA

“I had the distinct pleasure of working with Matt at nVisia. Matt's understanding of the Microsoft Technical space is outstanding. He is constantly working on improving his technical skills and rapidly masters any new technology that he encounters. He is an excellent teacher and a wonderful asset for any size team.” 
Jim Harnden , Senior Technical Architect , nVISIA

“Matt Warren is a very talented developer with great capacity for self study, investigation and adapts to new languages and frameworks with ease. He has an excellent grasp of software architecture and modern development principles. He has proven himself time and time again to be a hard worker and someone who can get the job done when you're in a tight spot.” 
Andrew Jump , Partner, C# Developer , Contegra

This website represents some of my spare time.  My small presence on the web between my family and my career.  I hope over time you find many useful things here.