Wednesday 11 December 2013

How to bind DropdownList with CountryName using CultureInfo Class……

How to bind DropdownList with Country Name using CultureInfo Class……

Note: how to bind dropdownlist without any  table data means country name not exist any specific table……

Source Code for Default.aspx …………………………….

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
         width: 100%;
              }
.auto-style2 {
width: 104px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td colspan="2"><font color="red"><b>How to bind DropdownList with CountryName using CultureInfo Class</b></font></td>
</tr>
<tr><td class="auto-style2">County Name</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" style="margin-left: 0px">
</asp:DropDownList>
</td></tr>
</table>
</div>
</form>
</body>
</html>

Source Code for Default.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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

DropDownList1.DataSource = CountryList();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "----Select----");

}

}
public static List<string> CountryList()
{
List<string> CultureList = new List<string>();
CultureInfo[] GetCultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo getculture in GetCultureInfo)
{
RegionInfo GetRegionInfo = new RegionInfo(getculture.LCID);
if (!(CultureList.Contains(GetRegionInfo.EnglishName)))
{
CultureList.Add(GetRegionInfo.EnglishName);
}
}
CultureList.Sort();
return CultureList;
}

}

1 comments: