Copied to clipboard

Flag this post as spam?

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


  • Scott Anderson 18 posts 51 karma points
    Nov 21, 2014 @ 13:45
    Scott Anderson
    0

    Custom DataType Country List - alternative to uComponents Country Picker

    Hi,

    I was using uComponents Country Picker but a heap of countries were missing from the list.

    So here is the code for a custom datatype of countries.

    In the /usercontrols folder add this code to CountryDropDown.ascx.cs

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    
    
    namespace MyProject
    {
        public partial class CountryDropDown : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
        {
            public string umbracoValue;
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Page.IsPostBack)
                {
                    umbracoValue = ddlCountry.SelectedValue;
                }
    
                var addedCountries = new List<string>();
                foreach (System.Globalization.CultureInfo cultureInfo in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.SpecificCultures))
                {
                    System.Globalization.RegionInfo regionInfo = new System.Globalization.RegionInfo(cultureInfo.LCID);
                    if (!addedCountries.Contains(regionInfo.EnglishName))
                    {
    
                        addedCountries.Add(regionInfo.EnglishName);
                    }
                }
    
                foreach (var c in addedCountries.OrderBy(q => q).ToList())
                {
                    ddlCountry.Items.Add(new ListItem(c, c));
                }
                ddlCountry.Items.Insert(0, new ListItem("", " - Country - "));
                if (!String.IsNullOrEmpty(umbracoValue)){
                    ddlCountry.SelectedValue = umbracoValue;
                }
    
    
            }
    
            public object value
            {
                get
                {
                    return umbracoValue;
                }
                set
                {
                    umbracoValue = value.ToString();
                }
            }
        }
    }
    

    Then in the CountryDropDown.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CountryDropDown.ascx.cs" Inherits="MyProject.CountryDropDown" %>
    <asp:DropDownList runat="server" ID="ddlCountry" />
    

    Remember to compile...

    Then in Developer section of the back end Right-click on DataTypes and select "Create".

    Give the DataType a name like "Country DropDown", then in the Property Editor List select "Umbraco Usercontrol Wrapper". Then Save and select your CountryDropDown.ascx user control in the User Control list. Select Database Datatype and Save.

    All done... now you can add this to your properties.

  • Scott Anderson 18 posts 51 karma points
    Nov 21, 2014 @ 13:59
    Scott Anderson
    0

    Please note...

    To get this control working in the Member Properties I had to change the value saving code from

    umbracoValue = ddlCountry.SelectedValue;
    

    to

    if (!String.IsNullOrEmpty(Request[ddlCountry.UniqueID]))
    {
        umbracoValue = Request[ddlCountry.UniqueID].ToString();
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft