preserving carriage returns

Discussion in 'ASP .Net' started by TheDude5B, Nov 1, 2005.

  1. TheDude5B

    TheDude5B Guest

    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; ?
     
    TheDude5B, Nov 1, 2005
    #1
    1. Advertisements

  2. TheDude5B

    Mr Newbie Guest

    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.
     
    Mr Newbie, Nov 1, 2005
    #2
    1. Advertisements

  3. TheDude5B

    Greg Burns Guest

    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
     
    Greg Burns, Nov 1, 2005
    #3
  4. TheDude5B

    TheDude5B Guest

    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
     
    TheDude5B, Nov 1, 2005
    #4
  5. TheDude5B

    Greg Burns Guest

    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 />")
     
    Greg Burns, Nov 1, 2005
    #5
  6. TheDude5B

    TheDude5B Guest

    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?
     
    TheDude5B, Nov 1, 2005
    #6
  7. TheDude5B

    TheDude5B Guest

    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
     
    TheDude5B, Nov 1, 2005
    #7
  8. TheDude5B

    Greg Burns Guest

    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
     
    Greg Burns, Nov 1, 2005
    #8
  9. TheDude5B

    sp3d2orbit Guest

    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
     
    sp3d2orbit, Nov 2, 2005
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.