Your specific issue is two fold:
1) You are setting m.getProperty("testField")
to the instance of the DropDownList
rather than to it's SelectedValue
2) As noted in the comments on the question you had a mis-match in your hardcoded DropDownList
values
and the values stored in the Umbraco DataType
for the testField
property.
You can address the second issue more robustly by binding your DropDownList
with what Umbraco calls
the PreValues
stored on the testField
DataType is will ensure the available options in the DropDownList
match the options defined on the DataType
As of writing this PreValues
are stored as an XML fragment on the datatype definition in
the following format:
<preValues>
<preValue id="1">Option 1</preValue>
<preValue id="2">Option 2</preValue>
<preValue id="3">Option 3</preValue>
</preValues>
To retrieve these programmatically is a little clunky (IMO); You first you have to obtain the Id
of DataType
by
mousing over the DataType
in the umbraco interface and looking in the status bar (you should see something like javascript:openDataType(1111)
where 1111
is the Id
.
Next call the umbraco.library.GetPreValues()
method, passing in the Id
obtained above. This method
returns an XPathNodeIterator
which you can use to get the values in whichever way suits your need such as creation of a Dictionary<string,string>
which you can bind to your DropDownList
. The following example is taken from Damiaan Peeters blog
and adapted ever so slightly.
private static Dictionary<string, string> GetPreValues(int dataTypeId)
{
XPathNodeIterator preValueRootElementIterator = umbraco.library.GetPreValues(dataTypeId);
preValueRootElementIterator.MoveNext(); //move to first
XPathNodeIterator preValueIterator = preValueRootElementIterator.Current.SelectChildren("preValue", "");
var retVal = new Dictionary<int, object>();
while (preValueIterator.MoveNext())
retVal.Add(preValueIterator.Current.GetAttribute("id", ""), preValueIterator.Current.Value);
return retVal;
}
Then in your code behind you can do the following:
tbOrganisation.DataSource = GetPreValues(1111);
tbOrganisation.DataTextField = "Value";
tbOrganisation.DataValueField = "Key";
tbOrganisation.DataBind();