When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Sunday, November 21, 1999

Correctly Displaying the Results from a TEXTAREA

If you need to let your visitors enter a large amount of textual information, using TEXTAREAs are the way to go. TEXTAREAs are those large, multi-row text boxes. For example, here is a TEXTAREA with 40 columns and 5 rows, created using the following HTML code:

- continued -


(For more information on TEXTAREA, see Microsoft's HTML Documentation.)

The user can enter information on several lines, and spaces and carraige returns can be used to format the text within the TEXTAREA. Imagine that we saved the results of the TEXTAREA into a database field, and, at some later time, wish to display that database field on a web page. When we attempt to display the results, they will all be on one line, and the spaces will be ignored! For example, say that someone enters into a TEXTAREA the following text:

My little horse must think it queer
  to stop without a farmhouse near
Between the wood and frozen lake,
  the coldest evening of the year.

If we attempt to display this (either directly when the form is submitted, or sometime later, if the results are stored in a database), the output on a web page will be:

My little horse must think it queer to stop without a farmhouse near Between the wood and frozen lake, the coldest evening of the year.

This is because when your browser renders HTML, it ignores whitespace. To signify a line break, you need to use the <BR> tag; to force a space, you need to use &nbsp;. A line break, when entered into a TEXTAREA is saved as a carraige-return-line-feed, vbCrLf. We can use the Replace function to replace all vbCrLfs with <BR> before we display the contents of the TEXTAREA. Similarly, we can replace all spaces with &nbsp;.

For example, if we create a TEXTAREA in a form, using the following code:


the form processing script DisplayTextArea.asp will be loaded when the user clicks submit. We can then display the value entered by the user in the TEXTAREA with the following line of code in DisplayTextArea.asp:

Response.Write Request.Form("Poetry")

However, this will not display the carraige returns and spaces entered by the user in the TEXTAREA. To show the spaces and carraige returns, we will need to use the Replace function. We will begin by reading the value of Request.Form("Poetry") into a string variable- we will then use the Replace functiont twice, once to replace vbCrLfs with <BR> and once to replace spaces with &nbsp;. Here is the code!

'Read in the value of the TEXTAREA Dim strPoetry strPoetry = Request.Form("Poetry") 'Replace all vbCrLf with <BR>s strPoetry = Replace(strPoetry, vbCrLf, "<BR>") 'Replace all spaces with &nbsp; strPoetry = Replace(strPoetry, " ", "&nbsp;") 'Output the formatted TEXTAREA value: Response.Write strPoetry

That's it! If you are storing the result of the TEXTAREA into a database, you can perform these steps before you insert the record, or each time you need to display the results on an HTML page. I'd recommend using the latter approach, since using the former approach mixes the data for your application with the presentation of your application, which is ill-advised.

Happy Programming!

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article