This tutorial will teach you how to create an ASP DropDownList that contains all countries using C#.  A country dropDownList can be used in many ways, such as when requiring information from the user when registering to your website or subscribing to a newsletter.

Create countries DropDownList in ASP.NET

First, add the System.Globalization namespace to your project:

using System.Globalization;

Then, add a DropDownList control to your page:

<asp:DropDownList ID="ddlCountries" runat="server"></asp:DropDownList>

Now use the function below to return a List object that contains all the counties of the world:

  
        public List GetCountryList()
        {
            List _list = new List();
            CultureInfo[] _cultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures |
                        CultureTypes.SpecificCultures);
            foreach (CultureInfo _cultureInfo in _cultures)
            {
                if (_cultureInfo.IsNeutralCulture || _cultureInfo.LCID == 127)
                {
                    continue;
                }

                RegionInfo _regionInfo = new RegionInfo(_cultureInfo.Name);

                if (!_list.Contains(_regionInfo.EnglishName))
                {
                    _list.Add(_regionInfo.EnglishName);
                }
            }
            _list.Sort();
            return _list;
        }
    }

Note that the loop continue to next iteration if the Culture is neutral or LCID is equal 127. Neutral cultures do not have regions, as well as LCID 127 which belong to the “Invariant Culture”.

Now call the function from the page_load even:

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            ddlCountries.DataSource = GetCountryList();
            ddlCountries.DataBind();
            ddlCountries.Items.Insert(0, "");

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Related Articles

Create SQL server database backup in ASP.NET using C#

Pass multiple CheckBox values from View to Controller in ASP.Net MVC

Send SMTP email in ASP.NET

Last modified: March 6, 2019

Comments

Write a Reply or Comment

Your email address will not be published.