Copied to clipboard

Flag this post as spam?

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


  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 08, 2010 @ 15:47
    Mike Chambers
    0

    empty option in the dropdownlisttype

    how can I remove the empty option that exists as the first item of the dropdownlisttype?

  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 08, 2010 @ 16:52
    Mike Chambers
    0

    So I worked around this by creating my own custom fieldType, just using the Contour.SharedSource.Providers.FieldTypes dropDownlist source as a starting point and removing the blank options.

    However, I notice that using this as a starting point I don't get the default value setting that is available on the dropdownlist field type... how can I get that back?

    Also, is there any mechanism for setting an "initial value" for the mandatory validator (like we have on a standard .net required validation control)

    I want to have a "please select one of the following" as the first item of the drop down, but don't want that to be seen as a valid choice by the validator, so like I say the equivalent of the initial value setting on the .net validator

    (we want this as just having the values in there with no select ends up with the form just being submitted with the first option left as selected all the time, end users heh!)

     

  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 08, 2010 @ 18:28
    Mike Chambers
    0

    not sure if this is correct... but couldn't find a way to hook into the defaultValue like with the deafult dropdownlist.

    any one care to comment?

     

     

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

     

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Nov 10, 2010 @ 13:32
    Tim
    0

    Hiya,

    I think the shared source code is a bit out of date, the default values were added to contour fairly recently, and I'm not sure if the shared source has been updated to include those in (although I could be wrong). It might be worth checking with either Tim G, or Per to see if they plan to update the shared source with that extra functionality.

    Cheers,

    Tim.

  • TaoistTotty 246 posts 314 karma points
    Nov 18, 2010 @ 15:23
    TaoistTotty
    0

    Have you had any luck with this Mike?

    Regards

    TT

  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 18, 2010 @ 15:59
    Mike Chambers
    0

    I've implemented a custom fieldtype along the lines above... I didn't actually need the deafultValue to function for my particular needs.

  • TaoistTotty 246 posts 314 karma points
    Nov 18, 2010 @ 16:31
    TaoistTotty
    0

    Many thanks Mike.

  • Seth Niemuth 275 posts 397 karma points
    Nov 10, 2011 @ 15:35
    Seth Niemuth
    0

    Has anyone had any luck with this? I am trying to be able to have 'Please select' instead of just blank for the first dropdown but still be able to use the default value.

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Nov 10, 2011 @ 15:41
    Tim
    0

    @Seth,

    Probably the simplest way to do this would be to use jQuery to hange the text on the first item in the list from blank to whatever you wanted using the onload event.

    :)

Please Sign in or register to post replies

Write your reply to:

Draft