Introduction:
Here I will explain how to bind all countries to dropdownlist in asp.net using c#, vb.net with example or get all countries or display countries in dropdownlist in asp.net using c#, vb.net. By using System.Globalization namespace we can get all countries list in asp.net using c#, vb.net.
Description:
In previous articles I explained Angularjs get dropdownlist selected value & text, display gridview based on dropdownlist selection in asp.net, bind dropdownlist in gridview in asp.net, gridview examples in asp.net and many articles relating to dropdownlist, gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to bind all countries to dropdownlist in asp.net using c#, vb.net with example.
In previous articles I explained Angularjs get dropdownlist selected value & text, display gridview based on dropdownlist selection in asp.net, bind dropdownlist in gridview in asp.net, gridview examples in asp.net and many articles relating to dropdownlist, gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to bind all countries to dropdownlist in asp.net using c#, vb.net with example.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>binding all countries
to dropdownlist in asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<b>Select Country:</b><asp:DropDownList ID="ddlCountries"
runat="server"/>
</div>
</form>
</body>
</html>
|
Now add following namespaces in code behind
C#
Code
using System;
using System.Web;
using System.Globalization;
using
System.Collections.Generic;
using
System.Web.UI.WebControls;
|
After completion of adding namespaces you need to write the
code like as shown below
protected void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> objcountries = new
List<string>();
CultureInfo[]
objculture = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo getculture in
objculture)
{
RegionInfo objregion
= new RegionInfo(getculture.LCID);
if
(!(objcountries.Contains(objregion.EnglishName)))
{
objcountries.Add(objregion.EnglishName);
}
}
objcountries.Sort();
ddlCountries.DataSource = objcountries;
ddlCountries.DataBind();
}
}
|
VB.NET
Code
Imports System.Web
Imports System.Globalization
Imports
System.Collections.Generic
Imports System.Web.UI.WebControls
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As EventArgs) Handles
Me.Load
If Not
IsPostBack Then
Dim objcountries As New List(Of String)()
Dim objculture As CultureInfo()
= CultureInfo.GetCultures(CultureTypes.SpecificCultures)
For Each getculture As CultureInfo In
objculture
Dim objregion As New RegionInfo(getculture.LCID)
If Not
(objcountries.Contains(objregion.EnglishName)) Then
objcountries.Add(objregion.EnglishName)
End If
Next
objcountries.Sort()
ddlCountries.DataSource = objcountries
ddlCountries.DataBind()
End If
End Sub
End Class
|
Demo
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
6 comments :
Nice sir
Thanks for this post.
It is really informative one. Get law essay writing service help.
Why it doesn't include 'Myanmar' ?
Excellent work. I was finding the same in different sites , but yours only fulfilled my requirement.
Could anyone tell how to store the selected country in MS Sql server?
hello can anyone tell me the code in above code to show select as first option in list item not a country name as first list item.
Note: Only a member of this blog may post a comment.