Programmatically populate DropDownList with Taxonomy Categories
Hi, I'm trying to create a custom widget whereby a dropdown list is populated with a series of regions, and when a region is selected a second dropdown list is populated with the countries within that region.
I’ve created a category ‘International Distributor Regions’ to list the Regions, and each of these Regions have Countries listed underneath them (please see attached)
I’ve used this code in my widget and can see ALL Categories in my DropDownList:
TaxonomyManager manager = TaxonomyManager.GetManager();var taxonomy = manager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Categories").SingleOrDefault();var details = taxonomy.Taxa.ToList();foreach (HierarchicalTaxon taxon in details)var li = new ListItem(taxon.Title, taxon.Title);ddlRegions.Items.Add(li);
You may want to try the following for your first drop down list.
TaxonomyManager manager = TaxonomyManager.GetManager();
var taxonomy = manager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name ==
"Categories"
).SingleOrDefault();
var intlRegion = taxonomy.Taxa.Where(t => t.Title ==
"International Distributor Regions"
).SingleOrDefault();
if
(intlRegion !=
null
)
foreach
(HierarchicalTaxon taxon
in
((HierarchicalTaxon)intlRegion).Subtaxa.OrderBy(t => t.Title)
var li =
new
ListItem(taxon.Title, taxon.Id);
ddlRegions.Items.Add(li);
TaxonomyManager manager = TaxonomyManager.GetManager();
var taxonomy = manager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name ==
"Categories"
).SingleOrDefault();
var region = taxonomy.Taxa.Where(t => t.Id ==
new
Guid(ddlRegions.SelectedValue)).SingleOrDefault();
if
(region !=
null
)
foreach
(HierarchicalTaxon taxon
in
((HierarchicalTaxon)region).Subtaxa.OrderBy(t => t.Title))
var li =
new
ListItem(taxon.Title, taxon.Id);
ddlCountries.Items.Add(li);
Fantastic, thanks!
Hi,
I am trying to populate dropdown lists with a list of areas (Toronto and Mississauga) and the list of cities within that area.
I used this code:
private void GetListOfAreas()
TaxonomyManager taxManager = TaxonomyManager.GetManager();
var areas = taxManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name.ToLower() == "cities").SingleOrDefault();
if (areas != null)
var orderedAreas = areas.Taxa.OrderBy(t => t.Name).Where( t => Visible == true);
foreach (var area in orderedAreas)
ListItem li = new ListItem(area.Title, area.Id.ToString());
listOfAreas.Items.Add(li);
I don't understand why the sub categories appear in the list. I was expecting it to show only Toronto and Mississauga. Could you please help?
private void GetListOfCities(string sArea)
TaxonomyManager taxManager = TaxonomyManager.GetManager();
var areas = taxManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name.ToLower() == "cities").SingleOrDefault();
var torontoAreas = areas.Taxa.Where(t => t.Title.ToLower() == sArea).SingleOrDefault();
if (torontoAreas != null)
foreach (HierarchicalTaxon taxon in ((HierarchicalTaxon)torontoAreas).Subtaxa.OrderBy(t => t.Title))
ListItem li = new ListItem(taxon.Title, taxon.Id.ToString());
listOfAreas.Items.Add(li);
The second method gives me an error. Please help?
Please see my category structure attached.
Thank you!
Hi,
If you have for example:
BaseDeparment
->SubDepartment1
->SubDepartment2
You need to use the code below to get the parent category and its sub categories.
TaxonomyManager manager = TaxonomyManager.GetManager();
var depatments = manager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name ==
"Categories"
).SingleOrDefault();
var baseTaxon = depatments.Taxa.Where(t => t.Title ==
"BaseDepartment"
).SingleOrDefault();
if
(baseTaxon !=
null
)
foreach
(HierarchicalTaxon t
in
baseTaxon.Taxonomy.Taxa.Where(taxa => taxa.Parent !=
null
&& taxa.Parent.Id == baseTaxon.Id))
//your logic