Notice: This site is no longer being updated!

For more recent posts, please visit my blog.

ControlBuilderFaq
   > Home
   > Search
   > About

Links
   > MSDN
   > ASP.net Forums

Search

Building custom designer action lists
by Eilon Lipton
7/17/2005 6:41:00 PM

In ASP.net 2.0 one of the new features custom controls can take advantage of is DesignerActionLists. A DesignerActionList allows the control to expose certain properties and methods in a special panel. For example, most data bound controls, such as the FormView control, expose a "Choose Data Source" task.

The fact is that writing your own action lists couldn't be simpler. Action lists are driven off of class metadata, so the first step is to to write a custom action list class, and add the appropriate metadata to it. The second step is to add your custom action list to the ActionLists property of your ControlDesigner.

Here's an example that shows how to expose a control's Text property through an action list:

namespace ControlBuilderFaq.Samples {    using System;    using System.ComponentModel;    using System.Web;    using System.Web.UI;    using System.Web.UI.Design;    using System.ComponentModel.Design;    [Designer(typeof(MyActionControlDesigner))]    public class MyActionControl : Control {        public string Text {            get {                object o = ViewState["Text"];                return (o == null ? String.Empty : (string)o);            }            set {                ViewState["Text"] = value;            }        }        protected override void Render(HtmlTextWriter writer) {            writer.Write(Text);        }    }    public class MyActionControlDesigner : ControlDesigner {        public override DesignerActionListCollection ActionLists {            get {                DesignerActionListCollection actionLists = new DesignerActionListCollection();                // Copy all the action lists from the base class                actionLists.AddRange(base.ActionLists);                // Add our own action list                actionLists.Add(new MyActionList((MyActionControl)Component));                return actionLists;            }        }        private sealed class MyActionList : DesignerActionList {            private MyActionControl _myActionControl;            public MyActionList(MyActionControl myActionControl)                : base(myActionControl) {                _myActionControl = myActionControl;            }            public string Text {                get {                    return _myActionControl.Text;                }                set {                    PropertyDescriptor propDesc = TypeDescriptor.GetProperties(typeof(MyActionControl))["Text"];                    propDesc.SetValue(_myActionControl, value);                }            }            public override DesignerActionItemCollection GetSortedActionItems() {                DesignerActionItemCollection items = new DesignerActionItemCollection();                items.Add(new DesignerActionPropertyItem("Text", "Text:"));                return items;            }        }    }}

The two key things to notice here are the ActionLists property that is being overridden in MyActionControlDesigner, and the GetSortedActionItems() method that is being overridden in MyActionList.

The previous example shows how to expose a simple property. You can also expose methods as tasks on your action list by adding a DesignerActionMethodItem to the collection. Methods will show up as links in the action panel. Boolean properties are also automatically exposed as checkboxes instead of textboxes.

To have slightly fancier properties, you can add TypeConverters and UITypeEditors to them. For example, if you want to offer the user some default values for the Text property, you could add a TypeConverter with a set of standard values to the Text property. Here's an example:

        private sealed class MyActionList : DesignerActionList {            private MyActionControl _myActionControl;            public MyActionList(MyActionControl myActionControl)                : base(myActionControl) {                _myActionControl = myActionControl;            }            [TypeConverter(typeof(TextTypeConverter))]            public string Text {                get {                    return _myActionControl.Text;                }                set {                    PropertyDescriptor propDesc = TypeDescriptor.GetProperties(typeof(MyActionControl))["Text"];                    propDesc.SetValue(_myActionControl, value);                }            }            public override DesignerActionItemCollection GetSortedActionItems() {                DesignerActionItemCollection items = new DesignerActionItemCollection();                items.Add(new DesignerActionPropertyItem("Text", "Text:"));                return items;            }            private sealed class TextTypeConverter : StringConverter {                public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {                    return true;                }                public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {                    string[] standardValues = new string[] { "Option 1", "Option 2", "Option 3" };                    return new StandardValuesCollection(standardValues);                }            }        }

In this example there is a custom type converter called TextTypeConverter that offers a collection of standard values so that the user will get a dropdown combobox instead of just a textbox:

- Eilon




Comments
How do you create collection editor?
by Brian
(Posted on 10/24/2005 7:40:00 PM)
How do you create an edit items task like the listbox control?
Collection editor
by Vidya
(Posted on 8/24/2006 5:23:00 PM)
Please let me know how to create a collection editor for smart tag in NET 2.0


Post a comment:
Name:  
E-mail:
(Optional; Will not be shown)
Title:
 
Content:  

(Maximum length 1000 characters. Newlines will be preserved, and HTTP URLs will become links automatically.)
Post Comment