You can add your own Smart Tasks to a custom control by inheriting a new class from the base DesignerActionList class. : WebControl « Custom Controls « ASP.NET Tutorial






using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;using System.ComponentModel;
using System.ComponentModel.Design;

namespace myControls
{
    [Designer(typeof(SmartImageDesigner))]
    public class SmartImage : WebControl
    {
        string _imageUrl;
        string _alternateText;
        int _rotation = 0;
        bool _mirror = false;

        public string ImageUrl
        {
            get { return _imageUrl; }
            set { _imageUrl = value; }
        }

        public string AlternateText
        {
            get { return _alternateText; }
            set { _alternateText = value; }
        }

        public int Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }

        public bool Mirror
        {
            get { return _mirror; }
            set { _mirror = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Img;
            }
        }        private string GetFilterString()
        {
            string _mirrorValue = "0";
            if (_mirror)
                _mirrorValue = "1";

            return String.Format("progid:DXImageTransform.Microsoft. BasicImage(Rotation={0},Mirror={1})", _rotation, _mirrorValue);
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.Filter, this. GetFilterString());
            writer.AddAttribute(HtmlTextWriterAttribute.Src, _imageUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Alt, _alternateText);


            base.AddAttributesToRender(writer);
        }
    }

    public class SmartImageDesigner : ControlDesigner
    {
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection actionLists = new DesignerActionListCollection();
                actionLists.AddRange(base.ActionLists);
                actionLists.Add(new SmartImageActionList(this));
                return actionLists;
            }
        }
    }

    public class SmartImageActionList : DesignerActionList
    {
        private DesignerActionItemCollection items;
        private SmartImageDesigner _parent;

        public SmartImageActionList(SmartImageDesigner parent)
            : base(parent.Component)        {
            _parent = parent;
        }

        public void Rotate()
        {
            TransactedChangeCallback toCall = new TransactedChangeCallback(DoRotate);
            ControlDesigner.InvokeTransactedChange(this.Component, toCall, "Rotate", "Rotate image 90 degrees");
        }

        public void Mirror()
        {
            TransactedChangeCallback toCall = new TransactedChangeCallback(DoMirror);
            ControlDesigner.InvokeTransactedChange(this.Component, toCall, "Mirror", "Mirror Image");
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            if (items == null)
            {
                items = new DesignerActionItemCollection();
                items.Add(new DesignerActionMethodItem(this, "Rotate", "Rotate Image", true));
                items.Add(new DesignerActionMethodItem(this, "Mirror", "Mirror Image", true));
            }
            return items;
        }

        public bool DoRotate(object arg)
        {
            SmartImage img = (SmartImage)this.Component;
            img.Rotation += 1;
            if (img.Rotation > 3)
                img.Rotation = 0;
            _parent.UpdateDesignTimeHtml();
            return true;
        }

        public bool DoMirror(object arg)
        {
            SmartImage img = (SmartImage)this.Component;
            img.Mirror = !img.Mirror;
            _parent.UpdateDesignTimeHtml();
            return true;
        }
    }
}








14.19.WebControl
14.19.1.A control that inherits from the WebControl class.
14.19.2.Displaying a table of HTML colors.
14.19.3.File: DropShadow.cs
14.19.4.Specifying the Containing WebControl Tag
14.19.5.View State and Control State
14.19.6.Supporting Control State
14.19.7.Processing Postback Data and Events
14.19.8.Handling Postback Events
14.19.9.contains the custom pager control.
14.19.10.Using Postback Options
14.19.11.Working with Control Property Collections
14.19.12.Using a ControlBuilder
14.19.13.Applying Design-Time Attributes to a Control
14.19.14.Creating a Container ControlDesigner
14.19.15.You can add your own Smart Tasks to a custom control by inheriting a new class from the base DesignerActionList class.
14.19.16.Your own data binding control