Custom Control. Type of custom property is array of string values

  1. #1

    Custom Control. Type of custom property is array of string values

    Hello,

    I'm trying to create custom ext.net control which based on Panel with custom property "Groups". This property should be an array of string values
    ( groups: ["aaa", "bbb"] ).
    I'de like to see next structure on design mode:

    <ux:MyPanel runat="server">
        <Groups>
            <ux:Group Name="aaa" />
            <ux:Group Name="bbb" />
        </Groups>
    </ux:MyPanel>
    Custom control codebehind:
    // Custom control
    public partial class MyPanel : Panel
    {
            private GroupCollection groups;
    
            [ConfigOption("groups", JsonMode.AlwaysArray)]
            [Category("Config Options")]
            [DefaultValue(null)]
            [PersistenceMode(PersistenceMode.InnerProperty)]
            [ViewStateMember]
            public virtual GroupCollection Groups
            {
                get
                {
                    if (this.groups == null)
                    {
                        this.groups = new GroupCollection();
                        this.groups.TrackViewState();
                    }
    
                    return this.groups;
                }
            }
    }
    
        public partial class MyPanel
        {
            [Browsable(false)]
            [EditorBrowsable(EditorBrowsableState.Never)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            [XmlIgnore]
            [JsonIgnore]
            public override ConfigOptionsCollection ConfigOptions
            {
                get
                {
                    ConfigOptionsCollection list = base.ConfigOptions;
    
                    list.Add("groupField", new ConfigOption("groups", new SerializationOptions("groups", JsonMode.AlwaysArray), null, this.Groups));
    
                    return list;
                }
            }
        }
    
    // Groups collection
        public partial class Group : StateManagedItem
        {
            [DefaultValue("")]
            [NotifyParentProperty(true)]
            [Description("The grouping fieldname")]
            public string FieldName
            {
                get 
                {
                    object obj = this.ViewState["FieldName"];
                    return obj != null ? (string)obj : string.Empty;
                }
                set 
                {
                    this.ViewState["FieldName"] = value;
                }
            }
    
            public override string ToString()
            {
                return FieldName;
            }
        }
    
        public class GroupCollection : StateManagedCollection<Group> { }
    But I get array of empty objects on client side ( groups: [{}, {}] ) or array of not empty objects (groups: [{group: 'aaa'}, {group: 'bbb'}]) if I override ConfigOptions for Group .

    How do I configure Groups property or classes to get an array of string on client side?
  2. #2
    Hi,

    Yury, welcome to Ext.NET!

    In such cases we create a proxy property, please look at the GroupsProxy property in the example below.

    Example Page
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <%@ Register Assembly="Work" Namespace="Work" TagPrefix="work" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.NET Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            <work:MyPanel runat="server">
                <Groups>
                    <work:Group FieldName="group1" />
                    <work:Group FieldName="group2" />
                </Groups>
            </work:MyPanel>
        </form>
    </body>
    </html>
    MyPanel.cs
    using System.ComponentModel;
    using System.Web.UI;
    using Ext.Net;
    
    namespace Work
    {
        public class MyPanel : Ext.Net.Panel
        {
            private GroupCollection groups;
    
            [DefaultValue(null)]
            [PersistenceMode(PersistenceMode.InnerProperty)]
            public virtual GroupCollection Groups
            {
                get
                {
                    if (this.groups == null)
                    {
                        this.groups = new GroupCollection();
                        this.groups.TrackViewState();
                    }
    
                    return this.groups;
                }
            }
    
            [DefaultValue(null)]
            private string[] GroupsProxy
            {
                get
                {
                    string[] value = null;
                    if (this.groups != null && this.groups.Count > 0)
                    {
                        int i = 0;
                        value = new string[this.groups.Count];
                        foreach (Group groups in this.groups)
                        {
                            value[i++] = groups.FieldName;
                        }
                    }
    
                    return value;
                }
            }
    
            public override ConfigOptionsCollection ConfigOptions
            {
                get
                {
                    ConfigOptionsCollection list = base.ConfigOptions;
    
                    list.Add("groups", new ConfigOption("groups", new SerializationOptions("groups", JsonMode.AlwaysArray), null, this.GroupsProxy));
    
                    return list;
                }
            }
        }
    
        public class Group : StateManagedItem
        {
            [DefaultValue("")]
            public string FieldName
            {
                get
                {
                    object obj = this.ViewState["FieldName"];
                    return obj != null ? (string)obj : string.Empty;
                }
                set
                {
                    this.ViewState["FieldName"] = value;
                }
            }
    
            public override string ToString()
            {
                return FieldName;
            }
        }
    
        public class GroupCollection : StateManagedCollection<Group> { }
    }
  3. #3
    Thanks, Daniil

Similar Threads

  1. [CLOSED] Custom type tool
    By caha76 in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: Jun 26, 2012, 7:24 AM
  2. [CLOSED] Hint in how to add Custom property to control
    By imaa in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Aug 17, 2011, 8:04 PM
  3. Replies: 1
    Last Post: Feb 28, 2011, 8:13 AM
  4. Replies: 5
    Last Post: Feb 14, 2011, 1:29 PM
  5. Custom Search ComboBox with String.Contains
    By BLOZZY in forum 1.x Help
    Replies: 0
    Last Post: Jan 28, 2011, 12:22 PM

Posting Permissions