More in this section

Forums / Developing with Sitefinity / Multi value user control propeties

Multi value user control propeties

4 posts, 0 answered
  1. Stephen
    Stephen avatar
    9 posts
    Registered:
    04 Jan 2008
    12 Jun 2008
    Link to this post

    Hi,  I have a user control to send email. I want to allow the administrator to set multiple email addresses on the control.
    My property looks like the following:
    How can I make itmulti value so I can enter multiple values in Sitefinity and bind it to my property in the user control?
    Thanks

    [
    Browsable(true)]

    [Category("Form Details")]

    public string Email_Address

    {

    get { return _sEmailAddress; }

    set { _sEmailAddress = value; }

    }

  2. Nigel
    Nigel avatar
    38 posts
    Registered:
    01 Dec 2004
    13 Jun 2008
    Link to this post
    Hi Stephen

    You could do it two ways, I think. (Maybe more?)

    The easy way is just have the admin enter emails in delimited list with a comma or semicolon delimiter. I believe you can provide some additional guidance to the user by adding a description like this above your property definition:
    [Description("For multiple addresses, separate with a comma.")]. This will show a little clickable question mark next to the control label.

    The more involved way is to use a WebEditor. I've got a WebEditor I've written for situations where I want a list entered. Code is below. First your property definition:

    [Browsable(true), WebEditor("Blix.Admin.WebEditors.StringArrayEditor, App_Code"), TypeConverter("Blix.Admin.WebEditors.StringArrayConverter, App_Code"), Category("Form Details")] 
            public string[] EmailAddresses 
            { 
                get 
                { 
                    return _emails; 
                } 
                set 
                { 
                    _emails = value; 
                } 
            } 

    And here is the WebEditor. Just put these classes into App_Code. You can create your own editing templates for it (using ascx controls). It's pretty straightforward if you take a look at the code. Hope it helps.


    using System; 
    using System.ComponentModel; 
    using System.ComponentModel.Design.Serialization; 
    using System.Globalization; 
    using System.Collections.Generic; 
    using System.Text; 
    using Telerik.Cms.Web.UI; 
    using Telerik.WebControls; 
    using System.Web.UI; 
    using System.IO; 
    using System.Web.UI.WebControls; 
    using Telerik.Cms; 
    using Telerik.Cms.Web; 
     
    namespace Blix.Admin.WebEditors 
        public class StringArrayEditor : WebUITypeEditor<string[]> 
        { 
            private List<string> enteredStrings; 
            private StringContainer container; 
            private ITemplate itemTemplate; 
            private TextBox stringarea; 
             
            public override string[] Value 
            { 
                get 
                { 
                    this.enteredStrings = new List<string>(this.getArray()); 
                    return this.enteredStrings.ToArray(); 
                } 
                set 
                { 
                    this.enteredStrings = new List<string>(value); 
                    this.ChildControlsCreated = false
                } 
            } 
     
            public ITemplate ItemTemplate 
            { 
                get 
                { 
                    return this.itemTemplate; 
                } 
                set 
                { 
                    this.itemTemplate = value; 
                } 
            } 
     
            public string ItemTemplatePath 
            { 
                get 
                { 
                    string value = (string)this.ViewState["ItemTemplatePath"]; 
                    return String.IsNullOrEmpty(value) ? "~/Sitefinity/Admin/ControlTemplates/Selectors/StringListCreator.ascx" : value; 
                } 
                set 
                { 
                    this.ViewState["ItemTemplatePath"] = value; 
                } 
            } 
     
            public StringContainer Container 
            { 
                get 
                { 
                    return this.container; 
                } 
            } 
     
            public TextBox StringArea 
            { 
                get 
                { 
                    return this.stringarea; 
                } 
            } 
     
            protected override void OnInit(EventArgs e) 
            { 
                base.OnInit(e); 
            } 
      
     
            protected override void CreateChildControls() 
            { 
                this.Controls.Clear(); 
     
                this.container = new StringContainer(this); 
     
                if (this.itemTemplate == null
                { 
                    if (this.Page != null
                    { 
                        string path = this.ItemTemplatePath; 
                        if (File.Exists(this.Page.MapPath(path))) 
                            this.itemTemplate = this.Page.LoadTemplate(path); 
                        else 
                            this.itemTemplate = new DefaultTemplate(); 
                    } 
                } 
                this.itemTemplate.InstantiateIn(this.container); 
     
                this.stringarea = this.container.StringArea; 
                if (this.stringarea != null
                { 
                    if (this.enteredStrings != null
                    { 
                        StringBuilder sb = new StringBuilder(); 
                        foreach (string s in this.enteredStrings) 
                        { 
                            sb.AppendLine(s); 
                        } 
                        this.stringarea.Text = sb.ToString().Trim(); 
                    } 
                    else 
                        this.enteredStrings = new List<string>(); 
                } 
                this.Controls.Add(this.container); 
            } 
     
            private string[] getArray() 
            { 
                if (!String.IsNullOrEmpty(this.stringarea.Text.Trim())) 
                { 
                    return this.stringarea.Text.Split(new char[] { '\r', '\n' } , StringSplitOptions.RemoveEmptyEntries); 
                } 
                else 
                    return new string[0]; 
            } 
     
             
            private class DefaultTemplate : ITemplate 
            { 
                public void InstantiateIn(Control container) 
                { 
                    Literal ltl = new Literal(); 
                    ltl.Text = "<p>Enter one per line.</p>"
     
                    TextBox txt = new TextBox(); 
                    txt.ID = "Textbox"
                    txt.Rows = 10; 
                    txt.Columns = 50; 
                    txt.TextMode = TextBoxMode.MultiLine; 
     
                    container.Controls.Add(ltl); 
                    container.Controls.Add(txt); 
                } 
            } 
     
            public class StringContainer : GenericContainer<StringArrayEditor> 
            { 
                private TextBox stringarea; 
     
                public StringContainer(StringArrayEditor owner) 
                    : base(owner) 
                { 
                } 
     
                public TextBox StringArea 
                { 
                    get 
                    { 
                        if (this.stringarea == null
                            this.stringarea = base.FindRequiredControl<TextBox>(); 
                        return this.stringarea; 
                    } 
                } 
                 
            } 
        } 
     
        public class StringArrayConverter : TypeConverter 
        { 
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
            { 
                if (sourceType != typeof(string)) 
                { 
                    return base.CanConvertFrom(context, sourceType); 
                } 
                return true
            } 
     
            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
            { 
                if (destinationType != typeof(InstanceDescriptor)) 
                { 
                    return base.CanConvertTo(context, destinationType); 
                } 
                return true
            } 
     
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
            { 
                if (!(value is string)) 
                { 
                    return base.ConvertFrom(context, culture, value); 
                } 
                if (string.IsNullOrEmpty((string)value)) 
                { 
                    return new List<string>().ToArray(); 
                } 
                return ((string)value).Split(new char[] { ';' }); 
            } 
     
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
            { 
                if (destinationType == null
                { 
                    throw new ArgumentNullException("destinationType"); 
                } 
                if ((destinationType != typeof(string)) || !(value is string[])) 
                { 
                    return base.ConvertTo(context, culture, value, destinationType); 
                } 
                StringBuilder sb = new StringBuilder(); 
                foreach (string str in (string[])value) 
                { 
                    sb.Append(str.ToString()); 
                    sb.Append(';'); 
                } 
                if (sb.Length > 1) 
                { 
                    sb.Remove(sb.Length - 1, 1); 
                } 
                return sb.ToString(); 
            } 
        } 
  3. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    13 Jun 2008
    Link to this post
    Hello Stephen,

    Currently, the Sitefinity control editor does not support editing properties which are Collections. The solution to your problem would be to have a string of comma-separated e-mails, and parse the string in the property of the control itself. Here is how you can do it with a private field:

        public string Emails 
        { 
            get 
            { 
                string result = String.Empty; 
                foreach (string email in this._emailArray) 
                { 
                    result += email + ','; 
                } 
                return result; 
            } 
            set 
            { 
                this._emailArray = value.Split(','); 
            } 
        } 
     
        private string[] _emailArray; 

    Of course, make sure that your field is persisted in ControlState and also validate e-mails before using them.

    Sincerely yours,
    Slavo
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  4. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    13 Jun 2008
    Link to this post
    Hello Stephen,

    It seems that Nigel was faster than me at providing a solution to your problem and I didn't see his post. Well, I've proposed only one of the ways he talks about, and his other suggestion is also a possibility and perfectly doable. What you choose to do is up to you. :)

    Regards,
    Slavo
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Register for webinar
4 posts, 0 answered