locked
a Composite control with design-time support RRS feed

  • Question

  • User61101408 posted

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

    Thursday, July 2, 2009 8:47 AM

Answers

  • User-2106054853 posted

    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;
                }
            }

          
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 6, 2009 4:20 AM