+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Programmatically populate DropDownList with Taxonomy Categories

Programmatically populate DropDownList with Taxonomy Categories

5 posts, 1 answered
  1. Cheryl
    Cheryl avatar
    3 posts
    Registered:
    07 Feb 2012
    06 May 2012
    Link to this post
    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.
  2. Richard Baugh
    Richard Baugh avatar
    201 posts
    Registered:
    22 Aug 2012
    08 May 2012
    Link to this post
    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.

    Answered
  3. Cheryl
    Cheryl avatar
    3 posts
    Registered:
    07 Feb 2012
    08 May 2012
    Link to this post
    Fantastic, thanks!
  4. Christina
    Christina avatar
    1 posts
    Registered:
    09 May 2013
    07 Jun 2013
    Link to this post
    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!
  5. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    21 Nov 2016
    12 Jun 2013
    Link to this post
    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
5 posts, 1 answered