Saturday, January 31, 2009

Use generics with drop down list

You can use generice to bind with drop down list. Suppose you have a class Country.

public class Country
{
int id;
string countryName;

public int ID
{
get { return id; }
}


public string CountryName
{
get { return countryName; }
set { countryName = value; }
}
}

List objCountry = new List();
objCountry = objCountry.FetchCountries();
ddlCountryList.DataSource = objCountry;
ddlCountryList.DataTextField = "CountryName";
ddlCountryList.DataValueField = "ID";
ddlCountryList.DataBind();

The ddlCountryList is an asp.net drop down list control.

No comments: