StrattonL
StrattonL

Reputation: 726

ASP.NET Replacing Line Break with HTML br not working

Hi I am trying to submit some information into a database using an ASP.NET multiline textbox. I have the following code:

Dim noteContent As String = Replace(txtNoteContent.InnerText.ToString(), vbCrLf, "<br />")
Shop.Job.Notes.addNote(Request.QueryString("JobID"), ddlNoteFrom.SelectedValue, ddlNoteTo.SelectedValue, noteContent)

The addNote's function code is:

Public Shared Sub addNote(ByVal JobID As Integer, ByVal NoteFrom As Integer, ByVal NoteTo As Object, ByVal NoteContent As String)
            Dim newNote As New Job_Note
            newNote.Date = DateTime.Now()
            newNote.JobID = JobID
            newNote.NoteByStaffID = NoteFrom
            If Not NoteTo = Nothing Then
                newNote.NoteToStaffID = CInt(NoteTo)
            End If

            newNote.NoteContent = NoteContent

            Try
                db.Job_Notes.InsertOnSubmit(newNote)
                db.SubmitChanges()
            Catch ex As Exception
            End Try

        End Sub

When it submit's the compiler does not seem to detect that line breaks have been entered into the textbox. I have debugged and stepped through the code, and sure enough, it just ignores the line breaks. If I try and replace another character instead of a line break, it works fine.

It doesn't seem to be anything to do with my function, the LINQ insert or anything like that, it simply just doesn't detect the line breaks. I have tried VbCr, and also chr(13) but they do not work either.

Can someone help me? I have been trying ad searching for over an hour trying to sort this.

Thanks

Upvotes: 1

Views: 6981

Answers (1)

Karl Nicoll
Karl Nicoll

Reputation: 16419

When you do your replace, you should check for VbCrLf (Windows Line Break), VbLf (Unix Line Break) and VbCr (Mac Line Break). If memory serves correct, the standard newline in a HTML textarea element is "\n" (aka VbLf), so you might just get away with replacing VbCrLf with VbLf in your code, but personally I always check for them all just to be safe.

Example

Dim htmlBreakElement As String = "<br />"
Dim noteContent As String = txtNoteContent.Text _
    .Replace(vbCrLf, htmlBreakElement) _
    .Replace(vbLf, htmlBreakElement) _
    .Replace(vbCr, htmlBreakElement)
Shop.Job.Notes.addNote(Request.QueryString("JobID"), ddlNoteFrom.SelectedValue, ddlNoteTo.SelectedValue, noteContent)

Upvotes: 2

Related Questions