Drop Down List Populator
- private void BuildLists() {
- BuildReferenceDropDownList(ddlLocation, ReferenceType.Location);
- BuildReferenceDropDownList(ddlBeltType, ReferenceType.BeltType);
- BuildReferenceDropDownList(ddlBusinessUnit, ReferenceType.BusinessUnit);
- BuildReferenceDropDownList(ddlAgency, ReferenceType.Agency);
- BuildReferenceDropDownList(ddlOrganization, ReferenceType.Organization);
- }
- private void BuildReferenceDropDownList(DropDownList ddl, ReferenceType referenceType) {
- List<Reference> refItems;
- refItems = referenceAdapter.GetAllByType(referenceType);
- foreach (Reference r in refItems) { ddl.Items.Add(new ListItem(r._shortAndLongName, r._referenceID.ToString())); }
- PrependDefaultDropDownListItem(ddl);
- }
- //-------------------------------------------------------------------------
- protected void PrependDefaultDropDownListItem(DropDownList ddl) {
- ListItem li = new ListItem("-- Select --", "-1");
- //li.Selected = true;
- ddl.Items.Insert(0, li);
- }
Check Box List Populator
- private void populateWants() {
- List<Reference> _refPAWant = new List<Reference>(); // Holds all PAs sought by user
- _refPAWant = _paAdapter.GetAllByUserIDAndType(user._employeeID, ProfessionalAttributeObjective.Seek, _professionalAttributeType);
- buildCheckBoxList(cblGoalsWant, _refPAGoals);
- populateCheckBoxList(cblGoalsWant, _refPAWant);
- }
- private void populateHas() {
- List<Reference> _refPAHave = new List<Reference>(); // Holds all PAs possessed by user
- _refPAHave = _paAdapter.GetAllByUserIDAndType(user._employeeID, ProfessionalAttributeObjective.Offer, _professionalAttributeType);
- buildCheckBoxList(cblGoalsHave, _refPAGoals);
- populateCheckBoxList(cblGoalsHave, _refPAHave);
- }
- private void buildCheckBoxList(CheckBoxList cbl, List<Reference> refItems) {
- foreach (Reference r in refItems) {
- cbl.Items.Add(new ListItem(r._shortName, r._referenceID.ToString()));
- }
- }
- private void populateCheckBoxList(CheckBoxList cbl, List<Reference> refItems) {
- foreach (Reference r in refItems) {
- if (cbl.Items.FindByValue(r._referenceID.ToString()) != null) { cbl.Items.FindByValue(r._referenceID.ToString()).Selected = true; }
- }
Once the gerbil in my head started running and I realized that both of this object derive from the ListControl object(ButtonList, ListBox, & RadioButtonList also derive from ListContol), it made my job of refactoring this a lot easier. I created a single method that accepts a ListControl and the Generic List in as arguments , then returns a ListControl back to the control that called it:
List Control Populator Method
- using System.Collections.Generic;
- using System.Web.UI.WebControls;
- using Models;
- namespace GenericNamespace
- {
- public class ListControlPopulator
- {
- public ListControl PopulateList(ListControl dll, List<Reference> reference)
- {
- if (dll.GetType().Equals(typeof(DropDownList)))
- dll.Items.Add(new ListItem("-- Select --", "-1"));
- foreach (Reference r in reference)
- {
- dll.Items.Add(new ListItem(r._shortAndLongName, r._referenceID.ToString()));
- }
- return dll;
- }
- }
- }
To call this method we made the code look for the dropdown like this:
Dropdown List Control - Now
- ListControlPopulator ddl = new ListControlPopulator();
- private void BuildLists()
- {
- ddlLocation = (DropDownList)ddl.PopulateList((ListControl)ddlLocation, referenceAdapter.GetAllByType(ReferenceType.Location));
- ddlBeltType = (DropDownList)ddl.PopulateList((ListControl)ddlBeltType, referenceAdapter.GetAllByType(ReferenceType.BeltType));
- ddlBusinessUnit = (DropDownList)ddl.PopulateList((ListControl)ddlBusinessUnit, referenceAdapter.GetAllByType(ReferenceType.BusinessUnit));
- ddlAgency = (DropDownList)ddl.PopulateList((ListControl)ddlAgency, referenceAdapter.GetAllByType(ReferenceType.Agency));
- ddlOrganization = (DropDownList)ddl.PopulateList((ListControl)ddlOrganization, referenceAdapter.GetAllByType(ReferenceType.Organization));
- }
And the Checkbox Control look like this:
Checkbox List Control - Now
- ListControlPopulator ddl = new ListControlPopulator();
- private void populateWants()
- {
- List<Reference> _refPAWant = new List<Reference>(); // Holds all PAs sought by user
- _refPAWant = _paAdapter.GetAllByUserIDAndType(user._employeeID, ProfessionalAttributeObjective.Seek, _professionalAttributeType);
- cblGoalsWant = (CheckBoxList)ddl.PopulateList((ListControl)cblGoalsWant, _refPAGoals);
- populateCheckBoxList(cblGoalsWant, _refPAWant);
- }
No comments:
Post a Comment