Display choicefield selected value in widget template

Posted by Community Admin on 04-Aug-2018 18:51

Display choicefield selected value in widget template

All Replies

Posted by Community Admin on 30-Apr-2012 00:00

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?

Posted by Community Admin on 30-Apr-2012 00:00

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] %>

also FYI, the team is aware of this issue and I'm told they are looking into ways to improve the choice field for many situations including this one. Once I know more I'll be sure to blog about it.

In the meantime, I hope this is helpful, thank you for your feedback!

Posted by Community Admin on 30-Apr-2012 00:00

Thank you! This was perfect!

Posted by Community Admin on 30-Apr-2012 00:00

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.

Posted by Community Admin on 30-Apr-2012 00:00

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[]

This thread is closed