Programmatically populate DropDownList with Taxonomy Categories - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Programmatically populate DropDownList with Taxonomy Categories

  • Programmatically populate DropDownList with Taxonomy Categories
  • 356711_Categories-List.png
    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);




    How do I populate my DropDownList with just the list of Regions (i.e. Asia, Carribean, Central America, Africa etc.) Then OnSelectedIndexChanged how do I populate the second DropDownList with just the list of Countries within the selected Region (i.e. if a user selects Asia from the first DDL, the second DDL would populate with Japan, Taiwwan, Indonesia etc.) 

    I’ve tried a few different ways of achieving this but nothing is working so far. If anyone coud point me in the right direction that would be fantastic. Thanks very much.
  • 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);
        

    Then when the user selects one of the items, you can then find all children based on the selected ID.
    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);
        

    Just make sure you add in some error trapping, but this should get your desired effect.

  • Fantastic, thanks!
  • a33f0a87-4cb5-6f77-96ac-ff0000ae3c70_cities.jpg
    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
                    
                


    Regards,
    Stefani Tacheva
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items