How to Limit a sentence’s length in datalist/gridview


We can use Substring to limit the total length of a sentence to a defined number.
C#:<asp:Label ID="LabelMore" runat="server" Text=’<%# Eval("Longtitle")==DBNull.Value ? "" :Eval("Longtitle").ToString().Substring(0,Math.Min(20,Eval("Longtitle").ToString().Length))+"…." %>‘></asp:Label>

VB.NET: <asp:Label ID="LabelMore" runat="server" Text=’<%# IIF(CONVERT.ToString(Eval("Longtitle"))="", "",Eval("Longtitle").ToString().Substring(0,Math.Min(20,Eval("Longtitle").ToString().Length))& "….") %>‘></asp:Label>

 
But when we want to limit the length by word count, the syntax is a little bit different.
You can use RegularExpressions to do it. Here I show you how to do it in string manipulation:
VB.NET/C#:
<asp:Label ID="Label1" runat="server" Text=’<%# String.Join(" ", (Eval("comments").ToString().Split()),0,Math.Min(20,Eval("comments").ToString().Split().Length))%>‘></asp:Label>
Function used: String.Join and String.Split.
Advertisements

One Comment on “How to Limit a sentence’s length in datalist/gridview”

  1. Sree Nath says:

    thanks, its very nice, save my time also…..this is what i want…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s