Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bambe 45 posts 66 karma points
    Feb 17, 2011 @ 02:50
    Bambe
    0

    How to set dropdown list default string

    Hi,

    the package datatype dropdown list default string is 'Choose...', I want modify then to other string, any idea?

    and the datatype (e.g.dropdown list) can I set the default value to it? I know prevalue can add a value, but then looks can not set the default value.

    thx alot :)

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Feb 17, 2011 @ 08:36
    Sebastiaan Janssen
    0

    Unfortunately, this cannot be done using the default dropdown list datatype, you'd have to write your own datatype to do this.

  • Mike Chambers 635 posts 1252 karma points c-trib
    Feb 17, 2011 @ 10:32
    Mike Chambers
    0

    I wanted one without the choose at all, so a user forced to chose an item from the get go...

    Something like...

        public class DropDownListNotEmpty : FieldType
        {
            private System.Web.UI.WebControls.DropDownList dd;
            private List<Object> _value;
            public DropDownListNotEmpty()
            {
                this.Id = new Guid("9255717A-BE66-4835-87DF-8B86CE3CA7C4");
                
                this.Name = "DropDownListNotEmpty";
                this.Description = "Renders a html select without an empty option";
                this.Icon = "dropdownlist.png";
                this.DataType = FieldDataType.String;
                this.AssociatedField = new Field();
                dd = new System.Web.UI.WebControls.DropDownList();
                _value = new List<object>();
            }

            public override List<object> Values
            {
                get
                {
                    if (!string.IsNullOrEmpty(dd.SelectedValue))
                    {
                        _value.Clear();
                        _value.Add(dd.SelectedValue);
                    }
                    return _value;
                }
                set
                {
                    _value = value;
                }
            }
            public override bool Rendered
            {
                get
                {
                    return base.Rendered;
                }
                set
                {
                    base.Rendered = value;
                }
            }
            public override System.Web.UI.WebControls.WebControl Editor
            {
                get
                {
                    //dd.Items.Add(new ListItem("", ""));
                    if (this.AssociatedField.PreValueSource != null)
                    {
                        foreach (var prevalue in this.AssociatedField.PreValueSource.Type.GetPreValues(this.AssociatedField))
                        {
                            ListItem li = new ListItem(DictionaryHelper.GetText(prevalue.Value), prevalue.Id.ToString());
                            dd.Items.Add(li);
                        }
                    }

                    if (_value.Count > 0)
                    {
                        dd.SelectedValue = DictionaryHelper.GetText(_value[0].ToString());
                    }
                    else
                    {
                        try
                        {
                            dd.Items.FindByText(DefaultValue).Selected = true;
                        }
                        catch { }
                    }


                    return dd;
                }
                set
                {
                    base.Editor = value;
                }
            }


            public override string RenderPreview()
            {
                return "<select class=\"dropdownlist\"></select>";
            }
            public override string RenderPreviewWithPrevalues(List<object> prevalues)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<select class=\"dropdownlist\">");
                //sb.Append("<option></option>");
                foreach (var prevalue in prevalues)
                {
                    sb.Append(string.Format("<option>{0}</option>", prevalue));
                }
                sb.Append("</select>");
                return sb.ToString();
            }
            public override bool SupportsPrevalues
            {
                get
                {
                    return true;
                }
            }
            [Umbraco.Forms.Core.Attributes.Setting("DefaultValue", description="Enter a default value", control="Umbraco.Forms.Core.FieldSetting.TextField")]
            public string DefaultValue { get; set; }
            [Umbraco.Forms.Core.Attributes.Setting("InitialValue", description = "Enter an initial value", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
            public string InitialValue { get; set; }
        }

    For your purposes think you'll need to add back in and modify the lines

    //dd.Items.Add(new ListItem("", ""));

     //sb.Append("<option></option>");

    (ones for front end admin, ones for the datatype editor)

Please Sign in or register to post replies

Write your reply to:

Draft