Click here to Skip to main content
15,940,271 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wan to bind all default countries to asp.net dropdown list control by using Json, so how to do it. I tried till like below.

/*Jquery*/

XML
<script type="text/javascript">
    $(document).ready(function() {
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Default.aspx/BindDatatoDropdown",
    data: "{}",
    dataType: "json",
    success: function(data) {
    $.each(data.d, function(key, value) {
    $("#ddlCountry").append($("<option></option>").val(value.CountryId).html(value.CountryName));
    });
    },
    error: function(result) {
    alert("Error");
    }
    });
    });
    </script>


/*C# webmethode*/

XML
[WebMethod]
            public static CountryDetails[] BindDatatoDropdown()
            {
                DataTable dt = new DataTable();
                List<CountryDetails> details = new List<CountryDetails>();


                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();


below cant get string values into datarow..so how to get?

C#
foreach (string dtrow in objcountries)
               {
                   CountryDetails country = new CountryDetails();
                   country.CountryId = Convert.ToInt32(dtrow["CountryId"].ToString());
                   country.CountryName = dtrow["CountryName"].ToString();
                   details.Add(country);
               }

               return details.ToArray();
   }
Posted
Comments
Herman<T>.Instance 9-Sep-15 3:37am    
You can set them in a LIST<> object. You can bind a LIST to a DropDownList control

1 solution

Try below code:
C#
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();

int CountryId = 0;
foreach (string dtrow in objcountries)
{
   CountryId += 1;
   CountryDetails country = new CountryDetails();
   country.CountryId = CountryId;
   country.CountryName = dtrow;
   details.Add(country);
}

Here I added one variable CountryId and increment inside foreach loop. Then assigning it to CountryDetails Object.

Note: One suggestion instead of looping every time you can store the country values datasource like XML or Database.
 
Share this answer
 
v3
Comments
Dawood507 9-Sep-15 7:59am    
Thanq very much..its working...
Iam not binding each time ..on page load i will bind.
Dawood507 11-Sep-15 3:33am    
but how to bind sortedly?
[no name] 11-Sep-15 3:53am    
Updated my answer. Please check.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900