protected void FillDropDown()
{
try
{
string Query = @"<Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where>";
DataTable dt = GetDropdownItems("ListName", Query);
BindDropdown(ddl, "Title", "Title", "", dt);
ListItem objListItem = new ListItem("-- Please Select --", "");
ddl.Items.Insert(0, objListItem);
}
catch (Exception ex)
{
ExceptionLog(ex);
}
}
private DataTable GetDropdownItems(string strListName, string strQuery)
{
DataTable dtSegment = null;
SPQuery query = new SPQuery();
query.Query = strQuery;
SPSite spCurSite = SPContext.Current.Site;
SPWeb web = spCurSite.OpenWeb();
SPList list = web.Lists[strListName];
SPListItemCollection itemColl = list.GetItems(query);
dtSegment = itemColl.GetDataTable();
return dtSegment;
}
private void BindDropdown(DropDownList ddl, string strDataTextField, string strDataValueField, string strDefaultVal, DataTable dtDropdownValues)
{
ddl.DataSource = dtDropdownValues;
ddl.DataTextField = strDataTextField;
ddl.DataValueField = strDataValueField;
ddl.DataBind();
}
{
try
{
string Query = @"<Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where>";
DataTable dt = GetDropdownItems("ListName", Query);
BindDropdown(ddl, "Title", "Title", "", dt);
ListItem objListItem = new ListItem("-- Please Select --", "");
ddl.Items.Insert(0, objListItem);
}
catch (Exception ex)
{
ExceptionLog(ex);
}
}
private DataTable GetDropdownItems(string strListName, string strQuery)
{
DataTable dtSegment = null;
SPQuery query = new SPQuery();
query.Query = strQuery;
SPSite spCurSite = SPContext.Current.Site;
SPWeb web = spCurSite.OpenWeb();
SPList list = web.Lists[strListName];
SPListItemCollection itemColl = list.GetItems(query);
dtSegment = itemColl.GetDataTable();
return dtSegment;
}
private void BindDropdown(DropDownList ddl, string strDataTextField, string strDataValueField, string strDefaultVal, DataTable dtDropdownValues)
{
ddl.DataSource = dtDropdownValues;
ddl.DataTextField = strDataTextField;
ddl.DataValueField = strDataValueField;
ddl.DataBind();
}