Wednesday, December 29, 2010

Custom ASP.NET control with inner properties

using System;
using System.Security.Permissions;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace aspnetCustom
{
    [TypeConverter(typeof(ExpandableObjectConverter)),]
    public class SubProperty
    {
        private string _Name = "";

        [DefaultValue(""), NotifyParentProperty(true),]
        public String Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }
    }

    [TypeConverter(typeof(ExpandableObjectConverter)),]
    public class Property
    {
        private string _Name = "";
        private List<SubProperty> _SubProperties = new List<SubProperty>();

        [DefaultValue(""), NotifyParentProperty(true),]
        public String Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(CollectionEditor), typeof(UITypeEditor)), PersistenceMode(PersistenceMode.InnerProperty),]
        public List<SubProperty> SubProperties
        {
            get
            {
                return this._SubProperties;
            }
        }
    }

    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),]
    public class Control : WebControl
    {
        private List<Property> _Properties = new List<Property>();
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(CollectionEditor), typeof(UITypeEditor)), PersistenceMode(PersistenceMode.InnerProperty),]
        public List<Property> Properties
        {
            get
            {
                return this._Properties;
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.DesignMode)
                writer.WriteLine("Control");
        }
    }
}

No comments:

Post a Comment