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 m开发者_StackOverflow社区ore, 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>
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) + "…";
}
}
}
Do you mean like...
int maxLength = 200;
string trimmed = (trimmed.length > maxLength) ? trimmed.Substring(0,maxlength) : trimmed ;
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
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
精彩评论