Converting SQL Server newline character to HTML break <br/>
A common scenario (at least for me) is the following:
1. Accept text input into a web form that includes multiline text boxes. The text often includes line breaks.
2. The text is stored in a database such as SQL Server 2005. SQL Server 2005 has no problem storing the lineĀ breaks.
3. I then display the data in an ASP.NET gridview control. Because the control renders as HTML, it doesn’t recognize the newline escape characters.
The problem is how to convert the newline characters into <br />. Here is my solution. It is closely based on this MSDN article: http://msdn2.microsoft.com/en-us/library/xwewhkd1.aspx (regex.replace)
The example assumes you are using a GridView with a TemplateField control. Inside the TemplateField control (ItemTemplate) is a label called lblSampleText.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments
No comments yet.
Leave a comment