First 150 characters..

Posted by Community Admin on 03-Aug-2018 16:29

First 150 characters..

All Replies

Posted by Community Admin on 05-May-2014 00:00

Hi,

Anyone know how to add a field to a widget template so that it pulls only the first few characters of the field? For example, we have a custom module that has Questions and Answers. I want to the master view to have a list of the questions with the first part of the answer.

 I tried

<asp:Label ID="Label1" runat="server" Text='<%# Eval("Answer").ToString().Substring(0,Math.Min(150,Eval("Answer").ToString().Length)) + "…" %>'></asp:Label>
But the field has HTML in it and it's causing formatting issues.

Posted by Community Admin on 05-May-2014 00:00

Sitefinity has an extension method to truncate

 Class is SitefinityExtensions

public static string TruncateString(this string valueToTruncate, int maxLength, SitefinityExtensions.TruncateOptions options)
So like Eval("Answer").ToString().TruncateString(100, SitefinityExtensions.TruncateOptions.IncludeEllipsis)

 

 

Posted by Community Admin on 05-May-2014 00:00

That's exactly what I needed. Thanks so much Steve!

Posted by Community Admin on 05-May-2014 00:00

Np, FYI...truncateoptions has 4 modes...I think they can stack (marked as flags)

 

            None
            FinishWord
            AllowLastWordToGoOverMaxLength
            IncludeEllipsis

This thread is closed