-
-
FillService to Populate ListControl Controls
by azamsharp on 5/21/2008 8:34:30 AM
-
Populating DropDownList, ListBox and other ListControl controls is simple but when you need to do it often then it becomes repetitive. Here is a method FillService that I use to populate the controls that inherit from ListControl class.
-
public static void FillService<T>(T control, IEnumerable dataSource, string dataTextField,
string dataValueField) where T : ListControl
{
FillService<T>(control, dataSource, dataTextField, dataValueField, String.Empty);
}
public static void FillService<T>(T control, IEnumerable dataSource, string dataTextField,
string dataValueField,string defaultText) where T : ListControl
{
control.DataSource = dataSource;
control.DataTextField = dataTextField;
control.DataValueField = dataValueField;
if (!String.IsNullOrEmpty(defaultText))
{
control.Items.Insert(0, new ListItem(defaultText, "0"));
}
control.DataBind();
}
And here is the usage:
IVRRepository repository = new VRRepository();
SiteHelper.FillService<DropDownList>(ddlCourses, repository.GetCoursesByDepartmentId(
Int32.Parse(ddlDepartments.SelectedValue)), "Name", "CourseID","Select a course");
IVRRepository repository = new VRRepository();
SiteHelper.FillService<ListBox>(lbSections, repository.GetSectionsByCourseId(
ddlCourses.SelectedValue.ToInt()), "SectionNo", "SectionID","Select a section");
-
Refactor it!
Please log in to refactor the code!
Login