///
/// Populates the drop down list box with given list type.
///
/// The dropdown list to be populated.
/// The data source.
/// The datatext field for the dropdown list.
/// The datavalue field for the dropdown list.
/// The selected value, if any.
/// Whether to add an empty value (e.g.--Select--).
public static void PopulateDropDownList(DropDownList dropDownList, IList dataSource, string dataText, string dataValue, string selectedValue, bool addEmptyValue)
{
if (dropDownList != null && dataSource != null)
{
if (dropDownList.Items != null)
{
dropDownList.Items.Clear();
}
dropDownList.DataSource = dataSource;
dropDownList.DataTextField = dataText;
dropDownList.DataValueField = dataValue;
dropDownList.DataBind();
if (addEmptyValue)
{
dropDownList.Items.Insert(0, new ListItem("--Select--", "-1"));
}
if (!string.IsNullOrEmpty(selectedValue))
{
dropDownList.SelectedValue = selectedValue;
}
}
}
Saturday, January 29, 2011
Generic method to populate dropdown list in asp .net
An attempt of making the dropdown list population easier for everyday projects :)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment