views:

64

answers:

4

I currently have a string that I want to limit to 200 characters.

I don't know how to format it so if it's less, it wont change, but if its more, it will trim it.

This is in a ListView Control, NOT a Repeater. Sorry for that, my mistake.

<ItemTemplate>
<div class="portfolio_title">
<div class="custom_title">
<%# DataBinder.Eval(Container.DataItem, "Title")%></div>
</div>
<asp:Literal ID="LiteralArticle" runat="server"></asp:Literal>
<%# DataBinder.Eval(Container.DataItem, "Article")%><br />
<a href="NewsFull.aspx?id=<%# DataBinder.Eval(Container.DataItem, "id")%>">Read Full Article...</a>
<div class="page_line">
</div>
</ItemTemplate>
+1  A: 

Do you mean like...

int maxLength = 200;
string trimmed = (trimmed.length > maxLength) ? trimmed.Substring(0,maxlength) : trimmed ;
ZombieSheep
+3  A: 

Here is some code that I use for this sort of thing. Attach this to the OnRowDataBound event. This truncates to 50 characters and adds elipses "...".

protected void CommentGridViewRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TableCell cell = e.Row.Cells[0];

            if (!string.IsNullOrEmpty(cell.Text) && cell.Text.Length > 50)
            {
                cell.Text = cell.Text.Substring(0, 50) + "&hellip;";
            }
        }
    }
Philip Smith
This is a reasonable approach. But depending on the layer the OP has, I would suggest making a new field on the Data model such as 'ShortBigText' and implement the shortening operation therein.
Noon Silk
@silky - An excellent point. I would make it a method, not a property, though as changing the `BigText` property would change the value returned from `ShortBigText`. That would violate property independence.
Philip Smith
+1  A: 

I assume this is going in a grid or something... I'd call a function and pass your Eval as an argument:

My example:

<asp:Image ID="imgTopLevelTickCross" runat="server" ImageUrl='<%# "/images/" &  getImage(Eval("DrwgID").toString()) & ".gif" %> ' />

The ImageURL calls getImage and passes the value of Eval("DrwgID") to it to form the src path

Public Function getImage(ByVal drwgID As Integer) As String


If TopLevelDrwgID = drwgID Then

        Return "True"
    Else
        Return "blank"
    End If
End Function
Markive
A: 

It's maybe a litte more than you need, but works well for me in most cases. It preserves the file ending, if you deal with files and adds "..." at the end of the shortend string if you want.

    /// <summary>
    /// Shortens a long string. Optionally keeps the file ending and adds a placeholder at the end.
    /// </summary>
    /// <example>
    /// Input:  ThisIsAVeryLongFilenameForThisTest.doc (length=10, placeholder='...', saveFileEnding=true)
    /// Output: ThisIsAVeryLong
    /// </example>
    /// <param name="value"></param>
    /// <param name="length"></param>
    /// <param name="placeHolder"></param>
    /// <param name="saveFileEnding"></param>
    /// <returns></returns>
    public static string ShowSummary(string value, int length, string placeHolder, bool saveFileEnding)
    {
        int lengthNew = length;
        string fileEnding = "";

        //nothing to do if the string is short enough
        if (length > value.Length)
        {
            return value;
        }

        if (saveFileEnding)
        {
            int index = value.LastIndexOf(".");

            if (index != -1)
            {
                fileEnding = value.Substring(index);
                lengthNew = length - fileEnding.Length;
            }
        }

        //substract the length of the placeholder
        lengthNew = lengthNew - placeHolder.Length;

        if (lengthNew > 0)
        {
            return value.Substring(0, lengthNew) + placeHolder + fileEnding;
        }
        else
        {
            //something is weird, maybe a really long filending or a '.' in the filename, so just cut it down 
            return value.Substring(0, length);
        }
    }//ShowSummary
Remy
There is a Win32 method to do this - `[DllImport("shlwapi.dll", CharSet = CharSet.Auto)]static extern bool PathCompactPathEx([Out] StringBuilder pszOut, string szPath, int cchMax, int dwFlags);`
Philip Smith
Didn't know that. Thanks for the input!
Remy