Saturday 6 February 2016

System.Globalization to bind all countries to Dropdownlist in asp.net.

System.Globalization

Description:-

In this example we explain that how to bind all countries to Dropdownlist in asp.net using System.Globalization. Or how to populate dropdown list with all country in asp.net.
Before use System.Globalization what is this?

System.Globalization is a namespace in .Net Framework and its contains various classes like country/region, calendars etc... So you can directly access it by using the system. Globalization namespace in your application.


 bindcountry.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="bindcountry.aspx.cs" Inherits="WebApplication1_bindcountry" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>bind all countries to dropdownlist in asp.net using System.Globalization in C#
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <b>Select Country:</b><asp:DropDownList ID="drpCountry" runat="server" />
    </form>
</body>
</html>


 bindcountry.aspx.cs:-

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

public partial class WebApplication1_bindcountry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CultureInfo[] objculture = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            List<string> objCountry = new List<string>();
            foreach (CultureInfo getculture in objculture)
            {
                RegionInfo objregion = new RegionInfo(getculture.LCID);
                if (!(objCountry.Contains(objregion.EnglishName)))
                {
                    objCountry.Add(objregion.EnglishName);
                }
            }
            objCountry.Sort();
            drpCountry.DataSource = objCountry;
            drpCountry.DataBind();
        }
    }
}


0 comments:

Post a Comment