ASP. NET Binding All Countries to DropdownList By VB.NET

In this article we will learn How to get list of all countries and bind to a DropDownList in ASP. NET.
  • 4464
 

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.


 

A.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)HandlesMe.Load

        DropDownList1.DataSource = GetCountry()

        DropDownList1.DataBind()

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

   End Sub

End Class

 

Now run the application and test it.


 


 

 B.gif

Figure2

 

Now select a country name from the DropDownList.


 

C.gif
 

 

Figure3

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.