a Composite control with design-time support

Last post 07-06-2009 4:20 AM by Allen Chen – MSFT. 1 replies.

Sort Posts:

  • a Composite control with design-time support

    07-02-2009, 8:47 AM
    • Member
      353 point Member
    • BaloonTiger
    • Member since 11-22-2003, 5:31 PM
    • Posts 110

    Do you know any good examples of a collection Editor in a composite control ?

  • Re: a Composite control with design-time support

    07-06-2009, 4:20 AM
    Answer

    Hi,

    From your description you want to use an editor to edit a property of your custom composite control, right? If so you can either use the built-in Editor, like below, or write your own editor (if you want to do so please use reflector to view the source code of existing editor to see how to write it)

    aspx:


            <cc1:MyControl ID="MyControl1" runat="server">
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
            </cc1:MyControl>

    aspx.cs:

      [ParseChildren(true, "Items")]
        public class MyControl : CompositeControl
        {
            public MyControl() {
                this.PreRender += new EventHandler(MyControl_PreRender);
            }

            void MyControl_PreRender(object sender, EventArgs e)
            {
                foreach (ListItem li in this.items)
                {
                    TextBox tb = new TextBox() { Text = li.Text };
                    this.Controls.Add(tb);
                }

            }

            private ListItemCollection items;

            [DefaultValue((string)null),
            PersistenceMode(PersistenceMode.InnerDefaultProperty),
            Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor,System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                typeof(UITypeEditor)), MergableProperty(false)]
            public virtual ListItemCollection Items
            {
                get
                {
                    if (this.items == null)
                    {
                        this.items = new ListItemCollection();
                     
                    }
                    return this.items;
                }
            }

          
        }

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (2 items)