ARTICLE

Binding All Countries to DropdownList in ASP. NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET June 27, 2011
In this article we will learn How to get list of all countries and bind to a DropDownList in ASP. NET.
Download Files:
 
Reader Level:

In this article we will learn How to get list of all countries and bind to a DropDownList in ASP. NET. Create an ASP. NET application and drag and drop a DropDownList control from the Toolbox to the page.

Add the following Namespace.

Imports System.Globalization

.ASPX code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="getcountryandbindwithdropdownlist.WebForm1" %>

 

<!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 runat="server">

   <title></title>

   <style type="text/css">

       .style1

        {

           color: #000099;

        }

   </style>

</head>

<body>

   <form id="form1" runat="server">

   <div>

       <span class="style1"><strong>Select Country :</strong></span>&nbsp;&nbsp;

       <asp:DropDownList ID="DropDownList1" runat="server" BackColor="#3399FF"

           ForeColor="#66FF66">

       </asp:DropDownList>

   </div>

   </form>

</body>

</html>

      

 

Page looks like below figure1.


ddl1.gif 

 

Figure1

 

CultureInfo class

 

The CultureInfo class contains culture-specific information, such as the language, country/region, calendar, and cultural conventions. The CultureInfo class specifies a unique name for each culture. The CultureInfo class specifies a unique name for each culture. 

 

 GetCultures method

 

GetCultures method to retrieve a complete list of all cultures.

 

Now call this GetCountry method on your page load that will bind and display countries in the DropDownList.

 

.VB Code

 

Imports System.Globalization

Public Class WebForm1

   Inherits System.Web.UI.Page

   Public Function GetCountry() As List(Of String)

       Dim list As New List(Of String)()

       Dim cultures As CultureInfo() =CultureInfo.GetCultures(CultureTypes.InstalledWin32CulturesOr CultureTypes.SpecificCultures)

       For Each info As CultureInfo In cultures

           Dim info2 As New RegionInfo(info.LCID)

           If Not list.Contains(info2.EnglishName) Then

                list.Add(info2.EnglishName)

           End If

       Next

       Return list

   End Function

   Protected Sub Page_Load(ByVal senderAs Object,ByVal e As System.EventArgs)Handles Me.Load

        DropDownList1.DataSource = GetCountry()

        DropDownList1.DataBind()

        DropDownList1.Items.Insert(0, "Select")

   End Sub

End Class

 

Now run the application and test it.


ddl2.gif

 

Figure2

 

Now select a country name from the DropDownList.


ddl3.gif

 

Figure3

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor