Reputation: 3379
I have a gridview that shows a user's name, a subject, and a date. The .aspx code looks like this:
<asp:BoundField DataField="UserName"
SortExpression="UserName" />
<asp:BoundField DataField="Subject"
SortExpression="Subject" />
<asp:BoundField DataField="Date"
SortExpression="Date" />
If the user enters a subject that is say 200 characters, I don't want it to all show otherwise the page will be stretched like crazy. Is there any way to do a check so that if the subject is more than say 60 characters, the rest is taken off and a ... is just appended on? So the subject would go from say:
This is a realllllllllllllllllllllllly long subject
to
This is a reallllll....
I know how to do it in the C# code behind but not sure how to manipulate it in the .aspx
Upvotes: 0
Views: 3147
Reputation: 18018
Try this:
<asp:TemplateField>
<ItemTemplate>
<%# ((string)Eval("Subject")).Length < 200? Eval("Subject") :((string)Eval("Subject")).Substring(0,200) + "..."%>)
</ItemTemplate>
</asp:TemplateField>
Alternatively (and more cleanly) you can (if possible) add a property to your datasource item-class and use that property in normal fashion. Say, your data-source item class is UserMail
; then add a public property say TruncatedSubject
like:
partial class UserMail
{
public string TruncatedSubject
{
get
{
return Subject.Length < 200? Subject : (Subject.SubString(0,200) + " ...");
}
}
}
then you can use it (TruncatedSubject
instead of Subject
) like a normal BoundField
column.
Upvotes: 4