ASP.NET使用HTML br替换换行符无效 [英] ASP.NET Replacing Line Break with HTML br not working

查看:680
本文介绍了ASP.NET使用HTML br替换换行符无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我正尝试使用ASP.NET多行文本框向数据库提交一些信息。
我有以下代码:

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

$ p $



addNote的函数代码是:公共共享Sub addNote(ByVal JobID作为整数,ByVal NoteFrom As整数,ByVal NoteTo As对象,ByVal NoteContent作为字符串)
Dim newNote作为新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()
赶上例外
结束尝试

结束小组

当它的提交的编译器似乎没有检测到已经输入到文本框中的换行符。我已经调试过了代码,确实,它只是忽略了换行符。如果我尝试替换另一个字符而不是换行符,它可以正常工作。



它似乎与我的函数,LINQ插入或类似的东西,它只是不检测换行符。我已经尝试过VbCr,并且也尝试过(13),但它们也不起作用。



有人可以帮我吗?



谢谢

解决方案

当你做替换时,你应该检查VbCrLf(Windows Line Break),VbLf(Unix Line Break)和VbCr(Mac Line Break)。如果内存服务正确,那么HTML textarea元素中的标准换行符是\\\
(又名VbLf),所以您可能会放弃将 VbCrLf VbLf 在您的代码中,但我个人总是检查它们都是为了安全。



示例

  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)


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

解决方案

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)

这篇关于ASP.NET使用HTML br替换换行符无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆