wordcount.aspx
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
myTextBox.Text = "This is some text for the textarea!"
End If
myPlaceholder.Visible = False
End Sub
Sub myButton_OnClick(Sender As Object, E As EventArgs)
Dim strInputText As String
strInputText = myTextbox.Text
' Echo back the input:
litInput.Text = Server.HTMLEncode(strInputText)
' Print out the counts:
litWords.Text = GetWordCount(strInputText)
litChars.Text = GetCharCount(strInputText)
myPlaceholder.Visible = True
End Sub
' I wrapped GetWordCount and GetCharCount into functions so you
' can reuse them in your own pages easily.
Function GetWordCount(strInput)
Dim strTemp
' Deal with tabs and carriage returns
' by replacing them with spaces.
strTemp = Replace(strInput, vbTab, " ")
strTemp = Replace(strTemp, vbCr, " ")
strTemp = Replace(strTemp, vbLf, " ")
' Remove leading and trailing spaces
strTemp = Trim(strTemp)
' Combine multiple spaces down to single ones
Do While InStr(1, strTemp, " ", 1) <> 0
strTemp = Replace(strTemp, " ", " ")
Loop
' Get a count by splitting the string into an array
' and retreiving the number of elements in it.
' I add one to deal with the 0 lower bound.
GetWordCount = UBound(Split(strTemp, " ", -1, 1)) + 1
End Function ' GetWordCount
Function GetCharCount(strInput)
GetCharCount = Len(strInput)
End Function ' GetCharCount
</script>
<html>
<head>
<title>ASP.NET Word Count Sample</title>
</head>
<body>
<form runat="server">
<asp:PlaceHolder id="myPlaceholder" runat="server">
<p>You entered:</p>
<pre>
<asp:Literal id="litInput" runat="server" />
</pre>
<p>
It contained
<b><asp:Literal id="litWords" runat="server" /></b> words and
<b><asp:Literal id="litChars" runat="server" /></b> characters.
</p>
<br />
</asp:PlaceHolder>
<p>Enter some text:</p>
<asp:TextBox id="myTextBox" runat="server"
TextMode = "MultiLine"
Columns = "40"
Rows = "5"
/><br />
<asp:Button id="myButton" runat="server"
Text = "Submit Query"
OnClick = "myButton_OnClick"
/>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/wordcount_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>