Nibble

Database driven dropdownlist datatype using DataEditorSettings

Just a quick post showing the impact of the DataEditorSettings attribute coming in umbraco Juno (making creating datatypes with custom settings super easy). If you attended the level 2 course this will look familiar, since in the course you learn how to create custom datatypes with the usercontrolwrapper, by making a dropdown that populates from a 3rd party db. Since with the usercontrolwrapper method you don’t have a way of setting up settings for your datatype you’ll basically hardcode the connectionstring, select statement, … Now in this example I also create a dropdown that can be populated with data from a db and in the setting of the datatype you simply set the connectionstring, select statement, text column (what will be shown in the dropdown) and value column (what will be saved).

Settings editor:

image

Datatype in action:

image

Complete sourcecode:

using System;
using System.Data;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.datatype;
 
namespace DataEditorSettings.Demo
{
    public class DbDrivenDropdown: AbstractDataEditor
    {
        private DropDownList control = new DropDownList();
 
        [DataEditorSetting("Connection string")]
        public string ConnectionString { get; set; }
 
        [DataEditorSetting("Select statement")]
        public string SelectStatement { get; set; }
 
        [DataEditorSetting("Text column")]
        public string TextColumn { get; set; }
 
        [DataEditorSetting("Value column")]
        public string ValueColumn { get; set; }
 
        public override Guid Id
        {
            get{return new Guid("833D755A-0A3C-4C06-8070-CA5E4BB9F68C");}
        }
 
        public override string DataTypeName
        {
            get{return "Db driven dropdown";}
        }
 
        public DbDrivenDropdown()
        {
            base.RenderControl = control;
            control.Init += 
                new EventHandler(control_Init);
            base.DataEditorControl.OnSave += 
                new AbstractDataEditorControl.SaveEventHandler(DataEditorControl_OnSave);
        }
 
        void DataEditorControl_OnSave(EventArgs e)
        {
            base.Data.Value = control.SelectedValue;
        }
 
        void control_Init(object sender, EventArgs e)
        {
       
            control.DataSource =
                    Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(
                    ConnectionString,
                    CommandType.Text,
                    SelectStatement);
            control.DataTextField = TextColumn;
            control.DataValueField = ValueColumn;
            control.DataBind();
 
            control.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            control.SelectedIndex = 0;
 
            if (base.Data.Value != null)
                control.SelectedValue = base.Data.Value.ToString();
        }
    }
}

1 Comment so far

  1. siva on August 7th, 2013

    Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(
    ConnectionString,
    CommandType.Text,
    SelectStatement);
    it is not working in umbraco 4.7.1.1?

Leave a Reply