preserving carriage returns

T

TheDude5B

Hi,

I have some data which is stored in my MySQL database as TEXT. when the
data is entered in, it has some carriage returns in it, and this can be
seen when querying the data using MySQL Query Browser.

I want to then display this text within <p> tags when requested from
the database. However, the test is formatted without the carriage
returns.

Is there a simple bit of code in which i can use to display the text
complete with its carriage returns, or do i have to have a bit of messy
code which will replace all the carriage returns with &vbCrLF; ?
 
M

Mr Newbie

You need to replace the CR's with <P> or <BR> 's in order to translate them
to HTML actionable code tags.

Use a RegEx to do this.

Regards Mr Newbie.
 
G

Greg Burns

I would think you would have to replace the carriage returns with <br>'s.

Dim TestString As String = "Shopping List"
' Returns "Shipping List".
Dim aString As String = Replace(TestString, "o", "i")

Greg
 
T

TheDude5B

I have used
Replace(post.Text, vbCrLf, "<br />")

and everything is working now, however, this is good for displaying my
text within the <p> tags. but when i view the text within a textbox,
for example when i want to edit the text, it shows up the <br /> tags
 
G

Greg Burns

Only do the replace when displaying the text in a label. Should give some
thought to using Server.HtmlEncode too.

Replace(Server.HtmlEncode(post.Text), vbCrLf, "<br />")
 
T

TheDude5B

ok i will not replace the vbcrlf when adding it to the database.
however i am unsure as to apply the replace code with my asp:repeater
controls.

i have the code for the line, which i am wanting to use the replace in,
as

<td colspan="2"
class="bordersides"><p><%#Container.DataItem("post_text")%></p></td>

how would i apply Replace( ,vbCrLf,"<br />") with this line
of code?
 
T

TheDude5B

i have got it now thanks.

used the code

<%#( DataBinder.Eval(Container.DataItem, "post_text")).Replace(vbCrLf,
"<br/>")%>

and this has worked.

Thanks for your Help Greg
 
G

Greg Burns

Another method would be to write a protected helper function in your code
behind.

something like this:

<%# MyHelperFunction(DataBinder.Eval(Container.DataItem, "post_text")))%>

Protected Function MyHelperFunction(ByVal value As Object) As String
If value Is DBNull.Value Then
Return ""
Else
Return value.ToString.Replace(vbCrLf,"<br/>")
End If
End Function

I find this kind of code necessary to protected myself from NULL values
coming back from db.

Greg
 
S

sp3d2orbit

Instead of doing a replace, a css attribute exists that you can use to
format the text automatically:

<div style="white-space: pre-line;">
Text goes here
</div>

Lookup the "white-space" css attribute for more info.

Best of luck,
Matt Furnari
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top