Home » .NET 4.0 » Content Limited by Word Count – VB.NET

Content Limited by Word Count – VB.NET


Imports System.Text.RegularExpressions

Partial Class for_sale_Default
    Inherits System.Web.UI.Page

Protected Sub GridView1_RowDataBound(ByVal sender As Object, 
                                        ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
   Dim WordCountInt As Integer = CountWords(DataBinder.Eval(e.Row.DataItem, 
                                                               "description").ToString)
   Dim WordCountStr As String = DataBinder.Eval(e.Row.DataItem, "description").ToString

  If WordCountInt >= 40 Then
    Dim iNextSpace As Integer = WordCountStr.LastIndexOf(" ", 175)
    e.Row.Cells(3).Text = + 
    DataBinder.Eval(e.Row.DataItem, "description").ToString.Substring(0, iNextSpace) + 
    " ... (<a href='Description.aspx?cat=1&aid=" + 
    DataBinder.Eval(e.Row.DataItem, "id").ToString + "' class='colorbox2'>more</a>)"
     e.Row.Cells(3).ToolTip = DataBinder.Eval(e.Row.DataItem, "description").ToString
  Else
     e.Row.Cells(3).Text = + DataBinder.Eval(e.Row.DataItem, "description").ToString
  End If
 End If
End Sub
    Public Function CountWords(ByVal value As String) As Integer
        ' Count matches.
        Dim collection As MatchCollection = Regex.Matches(value, "\S+")
        Return collection.Count
    End Function


End Class