Display choicefield selected value in widget template
I have created a custom module containing a choicefield. In designing the widget template, I want to be able to display the selected value of this choicefield for each item. A simple <%# Eval("ChoiceFieldName")%> tag in the widget template renders System.String[]. How do I retrieve the selected value of this field as a string?
Hi Craig, the value type of a ChoiceField is an array. this is because a choicefield has the option to allow multiple or single selection. If you require a single value, the property should be saved as a 1 element array, which you should be able to retrieve using index 0.
You'll need to cast this to a string array first of course
<%# ((string[])Eval("ChoiceField"))[0] %>
Thank you! This was perfect!
One last question for you on a related note... How do I then retrieve multiple values if a choice contains more than one? Thanks again.
In the case of a multiple selection, the array would be populated with the selected items, and you can either retrieve them by index or using something like foreach. Just be sure to cast the value to a string[]