从 <textarea> 获取文本时保留换行符
Keep linebreaks when getting text from <textarea>
我正在使用带有 C# 和 HTML 的 Visual Web Developer 构建一个站点。
我有一个页面,用户可以在 textarea 标签中写下关于我网站的反馈,然后提交(在 textarea 中,他们可以在任何地方换行)。
问题是当我取回他们写的文本时,它没有换行符,例如:
如果用户写道:
"Hello, my name is
Omer N."
当我取回它时,它看起来像这样:"Hello, my name is Omer N."。
我该如何解决这个问题?
取决于您存储值的方式。请记住,HTML 和遵循白色 space 规则的字段的一般输入,它将 truncate/condense 白色 space 合并为一个实体。
So "Wide String" = "Wide String" and:
"Multi-line
string
here" will be truncated to "Multi-line string here" as you have experienced.
这是默认行为。
因此,要保留换行符、间距等。您需要 escape it or a process of encoding and decoding,然后再存储它。
说明here:
Many newcomers to web development cannot get their head around why the
carriage returns they made in their data on input from a textarea, or from a
text file, Excel spreadsheet etc. do not appear when the web page renders.
The solution is fairly obvious once the newcomer realizes that a web
page is only the browser's interpretation of html markup, and that a
new line in html is represented by the
tag. So what is needed
is a way to swap carriage returns or line feeds with the
tag.
Well, a way to Replace() them, actually.
<%# Eval("MyMultiLineValue").ToString().Replace(<linebreak>,"<br />") %>
The string.Replace() method allows this, but we also need to identify
what we want to replace with the html tag. How is a new line
represented in C# or VB.Net?
In C#, it's "\r\n", while in VB.Net, it's vbcrlf. However, there is
also a language independent option that does just the same thing:
Environment.NewLine.
<%# Eval("MyMultiLineValue").ToString().Replace(Environment.NewLine,"<br />") %>
希望对您有所帮助! :)
我正在使用带有 C# 和 HTML 的 Visual Web Developer 构建一个站点。
我有一个页面,用户可以在 textarea 标签中写下关于我网站的反馈,然后提交(在 textarea 中,他们可以在任何地方换行)。
问题是当我取回他们写的文本时,它没有换行符,例如:
如果用户写道:
"Hello, my name is
Omer N."
当我取回它时,它看起来像这样:"Hello, my name is Omer N."。
我该如何解决这个问题?
取决于您存储值的方式。请记住,HTML 和遵循白色 space 规则的字段的一般输入,它将 truncate/condense 白色 space 合并为一个实体。
So "Wide String" = "Wide String" and:
"Multi-line
string
here" will be truncated to "Multi-line string here" as you have experienced.
这是默认行为。
因此,要保留换行符、间距等。您需要 escape it or a process of encoding and decoding,然后再存储它。
说明here:
Many newcomers to web development cannot get their head around why the carriage returns they made in their data on input from a textarea, or from a text file, Excel spreadsheet etc. do not appear when the web page renders.
The solution is fairly obvious once the newcomer realizes that a web page is only the browser's interpretation of html markup, and that a new line in html is represented by the
tag. So what is needed is a way to swap carriage returns or line feeds with the
tag. Well, a way to Replace() them, actually.
<%# Eval("MyMultiLineValue").ToString().Replace(<linebreak>,"<br />") %>
The string.Replace() method allows this, but we also need to identify what we want to replace with the html tag. How is a new line represented in C# or VB.Net?
In C#, it's "\r\n", while in VB.Net, it's vbcrlf. However, there is also a language independent option that does just the same thing: Environment.NewLine.
<%# Eval("MyMultiLineValue").ToString().Replace(Environment.NewLine,"<br />") %>
希望对您有所帮助! :)