umbraco datatypes and text value from code behind
I am trying to get the text value of a dropdown list from the API and I am seriously struggling.
This is what I have at the moment:
Document doc = new Document(Node.GetCurrent().Id);
doc.GetProperty("fieldPropertyName"开发者_Python百科).Value;
This returns a string representation of the id for the prevalue.
What I want is the text for that pre value.
Thanks in advance for the help.
Use the library function...
var stringValue = umbraco.library.GetPreValueAsString(Convert.ToInt32(doc.GetProperty("fieldName").Value));
Please excuse this being in VB.
This is the language I happened to be developing in. (How I wish I could use C#)
Imports System.Runtime.CompilerServices
Imports umbraco.cms.businesslogic.web
Imports umbraco.cms.businesslogic.datatype
Module UmbracoExtensionHelper
<Extension()>
Public Function GetCustomPropertyValueFromPreValues(ByVal doc As Document, ByVal propertyName As String)
Dim returnValue As String = ""
Dim objProperty As umbraco.cms.businesslogic.property.Property = doc.getProperty(propertyName)
If objProperty IsNot Nothing Then
Dim objPreValues = PreValues.GetPreValues(objProperty.PropertyType.DataTypeDefinition.Id)
If objPreValues IsNot Nothing Then
''run through the ids of the datatypes and the value of the property
For Each entry As DictionaryEntry In objPreValues
Dim currentPreValue As PreValue = CType(entry.Value, PreValue)
If currentPreValue.Id.ToString().ToLower() = objProperty.Value.ToString().ToLower() Then
returnValue = currentPreValue.Value.ToLower()
Exit For
End If
Next
End If
End If
Return returnValue
End Function
End Module
use following code
aspx page
<asp:DropDownList ID="ddlLocation" ClientIDMode="Static" runat="server" AutoPostBack="true" CssClass="selectbox" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" />
Code Behind
var regionItems = regionFolder.Children;
if (regionItems.Count > 0) {
foreach (Node region in regionItems) {
if (region.GetProperty(FieldName.REGIONNAME) != null && !string.IsNullOrEmpty(region.GetProperty(FieldName.REGIONNAME).Value)) {
ddlLocation.Items.Add(new ListItem(region.GetProperty(FieldName.REGIONNAME).Value, region.Id.ToString()));
}
}
}
//ddlLocation.Items.Insert(0, "Choose");
ddlLocation.Items.Insert(0, new ListItem("Choose", "0"));
here REGIONNAME= our field name,
精彩评论