TextBox 或 Html 区域中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/296849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Line Break in TextBox or Html Area
提问by deadbug
What is the way of separating Line-Breaks and Space in MultiLine Textbox or HTML Text area while inserting database (Asp.Net). For example what should I do if I want to insert
插入数据库(Asp.Net)时在多行文本框或HTML文本区域中分隔换行符和空格的方法是什么。例如,如果我想插入我该怎么做
Hello
你好
World
世界
to database with line breaks? And also While displaying what should I do? I don't want to use Editor for it.
到带换行符的数据库?并且在显示时我应该怎么做?我不想为此使用编辑器。
But the problem is how to seperate line break and space. Now when I insert
但问题是如何分离换行符和空格。现在当我插入
Hello
你好
World
世界
or hello world, it is saved with same character in the database. Therefore I can't separate them and I am showing hello world although user uses line breaks such as above. The value, which comes from TextBox or html area, are the same as with line break or space.
或 hello world,它在数据库中以相同的字符保存。因此,我无法将它们分开,尽管用户使用了上述换行符,但我正在显示 hello world。取自 TextBox 或 html 区域的值与换行或空格相同。
回答by deadbug
The line breaks should persist to the db. When you retreive the info and display as Html just replace the line break (i.e. Environment.NewLine) with a line break tag (i.e. < b r / >
)
换行符应该持续到数据库。当您检索信息并显示为 Html 时,只需将换行符(即 Environment.NewLine)替换为换行符标记(即< b r / >
)
回答by Jazz
< br />
didnt work for me. (while assigning text to html textarea)
< br />
没有为我工作。(同时将文本分配给 html textarea)
\r\n
did.
\r\n
做过。
回答by Handprint
Using Razor (MVC3), to display or preserve line breaks pulled from my db, the following works:
使用 Razor (MVC3),显示或保留从我的数据库中提取的换行符,以下工作:
@Html.Raw(text_to_display.Replace(Environment.NewLine, "<br/>"))
回答by David R Tribble
A few choices:
• Convert newlines to <br/>
.
• Put your text in a <pre>
block.
• Put each line in its own <div>
block.
一些选择:
• 将换行符转换为<br/>
.
• 将您的文本放在一个<pre>
块中。
• 将每一行放在自己的<div>
块中。
Remember, all whitespace (SP, HT, LF, CR) is ignored in HTML (most of the time).
请记住,所有空白(SP、HT、LF、CR)在 HTML 中(大部分时间)都被忽略。
What you store in your database should be the pure character text. Converting it into proper HTML for display requires replacing certain characters with HTML sequences.
您存储在数据库中的应该是纯字符文本。将其转换为合适的 HTML 以供显示需要用 HTML 序列替换某些字符。
回答by Mick
When trying to keep formatted carriage returns / new lines for a textarea control on asp.net page that gets it value from the code behind you should use:
当试图在 asp.net 页面上为 textarea 控件保留格式化的回车符/新行时,从后面的代码中获取它应该使用的值:
textarea.Value = Server.HtmlDecode(myValue.Replace("\r\n", " "));
This will render from:
这将从:
some blah blah\r\nblah blah
to:
到:
some blah blah
blah blah
So when you use javascript to read for line breaks you can use:
因此,当您使用 javascript 读取换行符时,您可以使用:
textarea.value.indexOf("\n");
回答by Chad Smith
You can use
您可以使用
回答by Old Web Guy
The only suggestion above that worked for me was: <& # 1 0 ;>
I am using a text box generated by Front Page now running on a Linux server.
上面唯一对我有用的建议是: <& # 1 0 ;>
我正在使用 Front Page 生成的文本框,现在在 Linux 服务器上运行。
Multiple uses of the string will not create multiple line breaks, but a single entry of the string on a new line provides a double line break suitable for a new paragraph.
字符串的多次使用不会创建多个换行符,但在新行上的字符串的单个条目提供适用于新段落的双换行符。
Before moving my page from a server using a Windows OS, I was able to accomplish Line Breaks by double "Enters."
在使用 Windows 操作系统从服务器移动我的页面之前,我能够通过两次“Enter”完成换行。
回答by Dan Powley
I always store user input as it is provided, but then you must always be very carefulwhen displaying it back again. Line breaks are simply a case of replacing System.Environment.Newline with "<br />". Not sure what the best practice is for preserving other white space when rendered as html, would be interesting to hear what other people do.
我总是按照提供的方式存储用户输入,但是在再次显示时必须非常小心。换行符只是将 System.Environment.Newline 替换为“<br />”的一种情况。不确定在呈现为 html 时保留其他空白的最佳做法是什么,听听其他人的做法会很有趣。
回答by Grayside
Retaining white space after rendering to HTML is most easily done by converting it into the HTML entity
.
在呈现为 HTML 后保留空白最容易通过将其转换为 HTML 实体来完成
。
回答by spacemonkeys
You need to replace all instances of vbcrlf (environment.newline) with the html tag [< b r >]
您需要用 html 标签 [<br>] 替换 vbcrlf (environment.newline) 的所有实例
And to allow multiple spaces you need to replace space with [& n b s p ;]
并且要允许多个空格,您需要用 [ ] 替换空格
That will convert your plain test to html "enough" to be viewed as html (works for me on the entered text sections of my web site)
这会将您的普通测试转换为“足够”的 html 以被视为 html(在我网站的输入文本部分对我有用)
From my tags it's only the text between the [] and remove the spaces ... placing the tag text into the page caused it to be displayed as the html !!!!
从我的标签中,它只是 [] 之间的文本并删除空格......将标签文本放入页面会导致它显示为 html !!!!