Link to home
Start Free TrialLog in
Avatar of markerasmus
markerasmusFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do you preserve line breaks from a textarea control to a asp:literal?

I am saving data with line breaks from a textarea control to a SQL Server 2008 database.  I have queried the DB and the data is being saved with square bracket characters representing line breaks.  When this data is binded back to a textarea control the line breaks are preserved.

However, if I bind the data to a asp:literal control the line breaks are not preserved.  

How do I preserve the line breaks when pulling data from SQL that was saved from a textarea control? I was thinking about do a replace for vbcrlf but the line breaks are being saved as square bracket characters rather than vbcrlf.
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Replace them with chr(10) and/or chr(13). See here: http://msdn.microsoft.com/en-us/library/ms187323.aspx

Cheers,

Lee
Avatar of markerasmus

ASKER

I tried that and it didn't work:

If Not ld.First.Profile Is Nothing Then litProfile.Text = ld.First.Profile.ToString.Replace(Chr(13) + Chr(10), "VbCrLf")

I also replacing Chr(13) and Chr(10) individually.  The square indicating line breaks still persists.

Any other ideas?
As long as your literal uses Mode=PassThrough (default) or Mode=Transform you could replace the linebreaks with "<br>" and it would insert an html linebreak.

Note: Mode="Encode" encodes your literal text to html and will not work for a <br> linebreak
Thanks, but how do I replace the square character with "<br />"?  chr(10) and chr(13) don't do it.
ASKER CERTIFIED SOLUTION
Avatar of nullcory
nullcory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks nullcory. ...ld.First.Profile.ToString.Replace(Chr(10), "<br />") worked.
You're going to have a lingering Carriage Return ("\r" or Chr(13)) but it should be of no consequence.
But if you want it gone you could do this
ld.First.Profile.ToString.Replace(Chr(13)+Chr(10), "<br />")
or
ld.First.Profile.ToString.Replace("\r\n","<br />")

\r is the escape sequence for Chr(13) (carriage return)
and \n is the escape sequence for Chr(10) (line feed)