Monday, December 5, 2011

Populating a Drop-Down with List Data

Steps:
1. Create a Drop-Down control,in a Webpart or Webapplication.
2. On the Load method, Add the following code.
protected override void OnLoad(EventArgs e)
{
if (!Page.IsPostBack)
{
DataSet ds = new DataSet();

SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();

SPList list = myWeb.Lists["ListName"];
DataTable DTable_List = list.Items.GetDataTable();
DTable_List.TableName = "Table1";
ds.Tables.Add(DTable_List);


DropDownBGroup.DataSource = ds.Tables["Table1"];
DropDownBGroup.DataTextField = "FieldName";
DropDownBGroup.DataValueField = "FieldName";
DropDownBGroup.DataBind();
DropDownBGroup.SelectedIndex = 0;}

base.OnLoad(e);

}
}

No comments:

Post a Comment