Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    Dec 29, 2011 @ 14:36
    Anthony Candaele
    0

    country picker in Contour

    Hi,

    Is there a way to have a country picker in a Contour form?

    Thanks for your help,

    Anthony

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 29, 2011 @ 14:46
    Tom Fulton
    1

    Hi Anthony,

    The easiest way would probably be to create a table in your database with the countries and use this as a prevalue source in Contour.  I created a wrapper FieldType for the uComponents Country Picker once but then found out that it doesn't include all countries, so I started using a static DB table instead.

    -Tom

  • Douglas Ludlow 210 posts 366 karma points
    Dec 29, 2011 @ 15:04
    Douglas Ludlow
    2

    This blog post gives a pretty good example on how to setup a prevalue source. It's either that or manually enter each country in... and that's not really an option.

  • Anthony Candaele 1197 posts 2049 karma points
    Dec 29, 2011 @ 16:05
    Anthony Candaele
    0

    @Tom, thanks for the advice, I'm using the uComponents Country Picker already, but I don't know how to create the wrapper FieldType for Contour.

    @Douglas, thanks Douglas for the information, I was looking for some more information on how to setup a prevalue source

    greetings,

    Anthony

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 29, 2011 @ 16:10
    Tom Fulton
    1

    Do you use Visual Studio?  I can paste the code I used to create the Country Picker fieldtype below, it's pretty simple - just need to add a reference to the Contour DLL.  Actually it doesn't use uComponents but uses the same methods to populate the dropdown.  Again the problem is that it is missing certain countries, but if you're OK with that it should work.  Otherwise the Prevalue Source is probably the best way :)

    [Umbraco.Forms.Core.Attributes.Setting("Choose Text", prevalues = "",
                description = "Enter the text to show as the default option in the dropdown (Default=Choose Country...)",
                control = "Umbraco.Forms.Core.FieldSetting.TextField")]
            public string ChooseText { get; set; }
           

            public List<Object> Value;
            public DropDownList ddlCountries;

            public CountryPicker()
            {
                Id = new Guid("3B41BB75-699D-42D4-8FD0-5E189E6E9588");
                Name = "Country Picker";
                Description = "A drop down list filled with countries";
                Icon = "textfield.png";
                DataType = FieldDataType.String;

                ddlCountries = new DropDownList();
                Value = new List<object>();

                ChooseText = String.IsNullOrEmpty(ChooseText) ? "Choose Country..." : ChooseText;

            }

            public override System.Web.UI.WebControls.WebControl Editor
            {
                get
                {
                    FillWithIsoCountries(ddlCountries);

                    if (Value.Count > 0)
                        ddlCountries.SelectedValue = Value[0].ToString();

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

            public override bool SupportsRegex
            {
                get { return false; }
            }

            public override bool SupportsPrevalues
            {
                get { return false; }
            }

            public override string RenderPreview()
            {
                return "<select><option>" + ChooseText + "</option></select>";
            }

            public override string RenderPreviewWithPrevalues(List<object> prevalues)
            {
                return RenderPreview();
            }

            public override List<object> Values
            {
                get
                {
                    Value.Clear();
                    Value.Add(ddlCountries.SelectedValue);
                    return Value;

                }
                set
                {
                    Value = value;
                }
            }

            /// <summary>
            /// Populates the list control with countries as given by ISO 4217.
            /// </summary>
            /// <param name="ctrl">The list control to populate.</param>
            /// <remarks>
            /// http://ucomponents.codeplex.com/SourceControl/changeset/view/75686#1458709
            /// http://www.prolificnotion.co.uk/c-utility-method-to-populate-list-controls-with-all-countries-as-given-in-iso-3166-1/
            /// </remarks>
            public void FillWithIsoCountries(ListControl ctrl)
            {
                var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

                var countries = cultures.Select(cultureInfo => new RegionInfo(cultureInfo.LCID)).Select(regionInfo => regionInfo.DisplayName).ToList();

                var sorted = (from c in countries
                              select c).Distinct().OrderBy(n => n);

                ctrl.Items.Add(new ListItem(ChooseText, string.Empty));

                foreach (string country in sorted)
                {
                    ctrl.Items.Add(new ListItem(country, country));
                }

            }

    -Tom

  • Anthony Candaele 1197 posts 2049 karma points
    Dec 29, 2011 @ 18:15
    Anthony Candaele
    0

    Hi Tom,

    Thanks a lot for the code. So I just add this as a class in a Visual Studio project and compile it?

    greetings,

    Anthony

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 29, 2011 @ 18:26
    Tom Fulton
    1

    Yep, add a class (CountryPicker) in VS, add a reference to Umbraco.Forms.Core.dll, paste the code, compile, then drop the DLL in your /bin/ folder.

  • Anthony Candaele 1197 posts 2049 karma points
    Dec 29, 2011 @ 18:31
    Anthony Candaele
    0

    ok, clear, you also mentioned that I should add a reference to the Contour.dll or is that the same as Umbraco.Forms.Core.dll

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 29, 2011 @ 18:32
    Tom Fulton
    0

    Yes, that's the one

  • Anthony Candaele 1197 posts 2049 karma points
    Dec 29, 2011 @ 18:32
    Anthony Candaele
    0

    ok, I'll let you know if it worked out.

    thanks

  • Anthony Candaele 1197 posts 2049 karma points
    Dec 29, 2011 @ 18:56
    Anthony Candaele
    0

    Hi Tom,

    I created my class, but still have some squiglies:

    the override methods aren't recognized ("no suitable method found to override")

    This are my using statements:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI.WebControls;

    using Umbraco.Forms.Core;

    using Umbraco.Forms.Core.FieldSetting.Pickers;

    using System.Globalization;

    Is it posible to give me the Using Statements you are using in your project?

    Thanks,

    Anthony

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 29, 2011 @ 19:26
    Tom Fulton
    0

    Sure, here you go, sorry about that.

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Web.UI.WebControls;

    using Umbraco.Forms.Core;

    Also your class needs to inherit from FieldType, ie:

    public class CountryPicker : FieldType
    {
    ... code from above ...
    }
  • Anthony Candaele 1197 posts 2049 karma points
    Dec 29, 2011 @ 19:40
    Anthony Candaele
    0

    HiTom,

    Thanks, problem was caused by not inheriting from Fieldtype. My code compiles now.

    Have been been thinking about it. Maybe for Contour it's more natural to use a Prevalue Source. I read the blog post on Prevalue Sources for Contour that Douglas was suggesting and it doesn't seem like a hard thing to implement. The only thing I'm wondering about, is how to create this Country table.

    I can't imagine someone typing in all those countries manualy :)

    Is there somewhere a Countries table, or a script that can be downloaded?

    If not, I'll give the wrapper class a go.

    greetings,

    Anthony

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 29, 2011 @ 19:42
    Tom Fulton
    0

    I agree, I don't use that fieldtype anymore as I said :)

    You can find lists on the web in different formats to import into your DB, here's a good start:  http://www.iso.org/iso/iso_3166_code_lists

  • Anthony Candaele 1197 posts 2049 karma points
    Dec 29, 2011 @ 19:44
    Anthony Candaele
    0

    Ok, thanks a lot

  • Anthony Candaele 1197 posts 2049 karma points
    Dec 29, 2011 @ 20:39
    Anthony Candaele
    0

    Hi guys,

    Mission accomplished. After taking some hurdles (importing the countries data from a flat file to my Countries table, and finding the correct syntax for my connectionstring) I was able to create the Countries prevalue source.

    Thanks to Douglas and Tom for their help!

    greetings,

    Anthony

Please Sign in or register to post replies

Write your reply to:

Draft