Populating DropDownList

In asp.net, you can populate the DropDownList in many ways.

1. You can add the contents to the DropDownList using coding as


DropDownList1.Items.Add("Fruits");
DropDownList1.Items.Add("Apple");
DropDownList1.Items.Add("Orange");

2. You can go to the property of the DropDownList.
There is a property called Items. In that you can directly give the items you want to add.

3. You can also add items to the DropDownList using the datasource.

DropDownList1.DataSource =datasource1;
DropDownList1.DataBind();


Comments

Author: Prakash kumar pothal18 Feb 2010 Member Level: Gold   Points : 2

if u Populating DropDownList dynamically.

Using query get all the data than from the database than bind the data in a dropdownlist
Dictionary Feet = CommonClass.BindHeight();
foreach (KeyValuePair KeyValuePair in Feet)
{
string text = KeyValuePair.Value;
string value = KeyValuePair.Key;
ddlID.Items.Add(new ListItem(text, value));
}

CommonClass -- This is class where u get the data through Dictionary
or

//--Bind the Blood type
List LstBloodType = CommonClass.BindBloodGroup();
ddlID.DataSource = LstBloodType;
ddlID.DataBind();

CommonClass -- This is class where u get the data through list and BindBloodGroup is function inthis class for get the results.


Regards,
prakash



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: