How to limit a line if it is more than necessary?

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

I don’t know how to format it, so if it is smaller, it will not change, but if it is more, it cuts it off.

This is a ListView control, not a relay. 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>
+3
source share
4 answers

Here is the code I use for this kind of thing. Attach this to the OnRowDataBound event. This reduces 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;";
            }
        }
    }
+3
source

You mean ...

int maxLength = 200;
string trimmed = (trimmed.length > maxLength) ? trimmed.Substring(0,maxlength) : trimmed ;
+1
source

, - ... Eval :

:

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

ImageURL getImage Eval ( "DrwgID" ), src

Public Function getImage(ByVal drwgID As Integer) As String


If TopLevelDrwgID = drwgID Then

        Return "True"
    Else
        Return "blank"
    End If
End Function
+1

It may be more than you need, but works well for me in most cases. It saves the end of the file if you are dealing with files and adds "..." at the end of the shortend line 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
+1
source

Source: https://habr.com/ru/post/1755926/


All Articles