Custom Property: Category Drop Down

by: Allan Thræn

Here's a request I've heard a couple of times: A DropDownList Property Control, with content that editors / administrators can control.
There's a built-in property control (AppConfigSettings), that can show a DDL, but it's based on settings in web.config, which makes it a bit more cumbersome for editors and administrators to modify the selection. Luckily we have the EPiServer Categories - an amazing, yet simple feature that has far from lived up to it's potential. Viewed correctly this can be the taxonomy you use to describe just about anything. For instance for defining the domain that you want your drop down list to be related to.

 

CatDropDown2

 

CatDropDown1Imagine you're designing a system where each page needs a property identifying which world-region it is intended for - and the requirement specs for the web site in their eternal wisdom demands that this list is customizable if a continent should disappear due to global warming :-) In that case it's easy to define a taxonomy branch with-in EPiServer Categories called "Region", with sub-categories for each region: "Europe", "North America", "Asia", ... however you know that the editors never look under the "Categories" tab when editing a page, and you feel that this is so important it should be displayed in a simple list on the "Information" tab. In comes the CategoryDropDownList to save the day. Add the property to your page-type and make sure to give it the same name as the category branch which children it should list.

 

CatDropDown3

The Custom Property is fairly simple and build on top of the TextBoxBase Property Control. The core of the logic is here:

protected System.Web.UI.WebControls.DropDownList ddl;

public override void CreateEditControls()
{
    ddl = new System.Web.UI.WebControls.DropDownList();
    Category main_c=EPiServer.DataAbstraction.Category.Find(CategoryDropDown.Name);
    ddl.Items.Add(new ListItem("", ""));
    foreach (Category c in main_c.Categories)
    {
        ListItem li = new ListItem(c.LocalizedDescription, c.Name);
        li.Selected = (((string)CategoryDropDown.Value) == c.Name);
        ddl.Items.Add(li);
    }
    
    this.ApplyControlAttributes(ddl);
    Controls.Add(ddl);
}

public override void ApplyEditChanges()
{
    if (ddl.SelectedValue != null)  SetValue(ddl.SelectedValue);
    else SetValue(null);
}

To use this, simply include the 2 source-files for it in your solution.Download them here. Enjoy!

14 March 2008

Tags:


    Comments

    1. I've done almost the same thing, but my solution is more targeted to developers/and more advanced administrators. Instead of using categories my property uses the help-text field, you can read about it here: http://antecknat.se/blog/2008/03/06/custom-property-dropdown/
    Post a comment    
    User verification Image for user verification  
    Allan Thræn

    About me

    I am a product manager @ EPiServer, with a passion for the more geeky side of things. My technical interests are typically focused around user problems, user experience,  search, information management, artificial intelligence and  personalization

    On top of this blog I have the blog Allan On Technology and I often crosspost.

    DISCLAIMER: Unless otherwise stated in the posts, this blog expresses my personal opinions, experiments and views, not necessarilly the views of EPiServer AB.

     2415 page views this week.

     

     

    Syndications


    Archive


    Tag cloud

    EPiTrace logger