Topic: Dropdown List - Binding With Code
C# DROPDOWN LIST
To display items from a data set in a ListBox, DropDownList, CheckBoxList, or RadioButtonList control, follow these steps:
1. Set the control's "DataSource" property to the name of the data set.
2. In the Page_Load event use the following code format
NOTE: If you are doing calls or updates from a control, you need to set the "AutoPostBack" control to True before it will execute.
// Fill the dataset
da_Whatever.Fill(ds_Whatever);
// drop down
if (!IsPostBack)
{
// For each row in the table
foreach ( ds_Whatever.ColumnNameWhateverRow rowItem in ds_Whatever1.TableWhatever )
{
// create a new list item
ListItem lstNew = new ListItem();
lstNew.Text = rowItem.FirstName + " " + rowItem.LastName;
// Add list item to drop-down list
lstNew.Value = rowItem.ColumnWhatever.ToString();
drp_Whatever.Items.Add(lstNew);
}
}