1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Display choicefield selected value in widget template

Display choicefield selected value in widget template

5 posts, 1 answered
  1. SVA Webmaster
    SVA Webmaster avatar
    85 posts
    Registered:
    18 Jul 2007
    30 Apr 2012
    Link to this post
    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?
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    30 Apr 2012
    Link to this post
    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!
    Answered
  3. SVA Webmaster
    SVA Webmaster avatar
    85 posts
    Registered:
    18 Jul 2007
    30 Apr 2012
    Link to this post
    Thank you! This was perfect!
  4. SVA Webmaster
    SVA Webmaster avatar
    85 posts
    Registered:
    18 Jul 2007
    30 Apr 2012
    Link to this post
    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.
  5. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    30 Apr 2012
    Link to this post
    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[]
5 posts, 1 answered