Copied to clipboard

Flag this post as spam?

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


  • Laura Holland 82 posts 103 karma points
    Sep 14, 2011 @ 20:56
    Laura Holland
    0

    Dropdown Datatype with list of U.S. States?

    I guess I'm lazy.. I thought there must be a package out there for this, but I can't find one. Anybody else know of a premade dropdown datatype listing U.S. states?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Sep 14, 2011 @ 21:53
    Lee Kelleher
    4

    Hi Laura,

    At least you admit you're being lazy. ;-)  I was curious to see if the U.S. States existed as a list in the .NET framework (similar to how countries are listed in the System.Globalization namespace) ... but alas it's not there!

    So using the super cool 'AbstractDataEditor', I've coded one up for you!

    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    using umbraco.cms.businesslogic.datatype;
    
    namespace Our.Umbraco.DataTypes
    {
        public class USStates : AbstractDataEditor
        {
            private DropDownList m_Control = new DropDownList();
    
            public IDictionary StateDictionary = new Dictionary
            {
                {"Alabama", "AL"},
                {"Alaska", "AK"},
                {"American Samoa", "AS"},
                {"Arizona", "AZ"},
                {"Arkansas", "AR"},
                {"California", "CA"},
                {"Colorado", "CO"},
                {"Connecticut", "CT"},
                {"Delaware", "DE"},
                {"District of Columbia", "DC"},
                {"Federated States of Micronesia", "FM"},
                {"Florida", "FL"},
                {"Georgia", "GA"},
                {"Guam", "GU"},
                {"Hawaii", "HI"},
                {"Idaho", "ID"},
                {"Illinois", "IL"},
                {"Indiana", "IN"},
                {"Iowa", "IA"},
                {"Kansas", "KS"},
                {"Kentucky", "KY"},
                {"Louisiana", "LA"},
                {"Maine", "ME"},
                {"Marshall Islands", "MH"},
                {"Maryland", "MD"},
                {"Massachusetts", "MA"},
                {"Michigan", "MI"},
                {"Minnesota", "MN"},
                {"Mississippi", "MS"},
                {"Missouri", "MO"},
                {"Montana", "MT"},
                {"Nebraska", "NE"},
                {"Nevada", "NV"},
                {"New Hampshire", "NH"},
                {"New Jersey", "NJ"},
                {"New Mexico", "NM"},
                {"New York", "NY"},
                {"North Carolina", "NC"},
                {"North Dakota", "ND"},
                {"Northern Mariana Islands", "MP"},
                {"Ohio", "OH"},
                {"Oklahoma", "OK"},
                {"Oregon", "OR"},
                {"Palau", "PW"},
                {"Pennsylvania", "PA"},
                {"Puerto Rico", "PR"},
                {"Rhode Island", "RI"},
                {"South Carolina", "SC"},
                {"South Dakota", "SD"},
                {"Tennessee", "TN"},
                {"Texas", "TX"},
                {"Utah", "UT"},
                {"Vermont", "VT"},
                {"Virgin Islands", "VI"},
                {"Virginia", "VA"},
                {"Washington", "WA"},
                {"West Virginia", "WV"},
                {"Wisconsin", "WI"},
                {"Wyoming", "WY"}
            };
    
            public override string DataTypeName
            {
                get
                {
                    return "U.S. States";
                }
            }
    
            public override Guid Id
            {
                get
                {
                    return new Guid("7B38C968-286E-4D3C-8C95-608CE3EA666C");
                }
            }
    
            public USStates()
            {
                base.RenderControl = this.m_Control;
    
                this.m_Control.Init += new EventHandler(this.m_Control_Init);
    
                base.DataEditorControl.OnSave += new AbstractDataEditorControl.SaveEventHandler(this.DataEditorControl_OnSave);
            }
    
            private void m_Control_Init(object sender, EventArgs e)
            {
                this.m_Control.DataSource = this.StateDictionary;
                this.m_Control.DataTextField = "Key";
                this.m_Control.DataValueField = "Value";
                this.m_Control.DataBind();
    
                if (base.Data.Value != null)
                {
                    this.m_Control.SelectedValue = base.Data.Value.ToString();
                }
            }
    
            private void DataEditorControl_OnSave(EventArgs e)
            {
                base.Data.Value = this.m_Control.SelectedValue;
            }
        }
    }

    Copy-n-paste this code into a .cs file and drop it into your App_Code folder - the Umbraco back-office should pick it up.

    I've posted this up on my GitHub too - for future reference: https://gist.github.com/1217575

    (Credit for the States list is from StackOverflow)

    Enjoy!

    Cheers, Lee.

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Sep 14, 2011 @ 22:22
    Hendy Racher
    1

    Hi, just a thought, but would an Enum of US states also be useful within any .net code ? if so (it's also less code) could just define an Enum and use the Enum DropDownList datatype.

    HTH,

    Hendy

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Sep 14, 2011 @ 22:24
    Lee Kelleher
    0

    An alternative (again in uComponents) is JSON DropDown ... that is, if you can find a reliable JSON data-source?

  • Laura Holland 82 posts 103 karma points
    Sep 15, 2011 @ 02:35
    Laura Holland
    0

    Wow, you guys are awesome. So many options, so little time..

  • Dan Roma 18 posts 38 karma points
    Aug 21, 2015 @ 21:00
    Dan Roma
    0

    This doesnt work for me. Am I missing something?

  • HuaweiP9User 31 posts 122 karma points notactivated
    May 03, 2018 @ 10:19
    HuaweiP9User
    0

    This doesn't work also for me. Any solution ?

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    May 03, 2018 @ 10:36
    Hendy Racher
    1

    Hi, if it's for v7, here's one approach to make a drop-down list of US States:

    http://www.proworks.com/blog/archive/simple-and-cool-use-for-the-nupickers-umbraco-package-for-v7/

  • HuaweiP9User 31 posts 122 karma points notactivated
    May 17, 2018 @ 21:48
    HuaweiP9User
    0

    Yeah, It's for v7 Let me try this as solution, but how can I implement this with class I dont want to use models, controllers.. I just want to find a way to use with some .cs file in which will be placed all states, and after that I need to render the state in a view (partial or just razor view). Any help ?

  • HuaweiP9User 31 posts 122 karma points notactivated
    May 18, 2018 @ 11:01
    HuaweiP9User
    0

    Okey, I try this but only I get is a full dropdown in backoffice, and I want to show that on front. (in view or in partial view)

    I try everything but no success. First I create Data Type and config the nuPicker as is shown on link. After that I could see all values in dropdown (backoffice). I add a composition with this data type and I want to show it on view as I told before.

    I try

        @Umbraco.AssignedContentItem.GetPropertyValue("aliasNameInComposition")
    
        @Umbraco.AssignedContentItem.GetProperty("aliasNameInComposition")
    
        @Umbraco.AssignedContentItem.GetPropertyValue("aliasNameInComposition").ToString()
    

    but no success. I want to dispaly dropdown with its value and after that need to use that dropdown for filtering data.

Please Sign in or register to post replies

Write your reply to:

Draft