问题
I have a textbox using multiline mode and when I create the stored procedure parameter for updating I use Server.HtmlEncode
on the textbox value. I can copy the content from the data record into Notepad and it shows the spaces where the user has pressed the Enter key for new paragraphs.
When displaying that content on a page (not a textbox for editing; just assigning the content to a Literal control), it has all the text run together.
I have done some searches about using Replace and different escape sequences or Environment.NewLine
. I am still confused about how to have the text display as the user entered it.
回答1:
Assuming you're using C# you can do the following when you display the string on the page
theStringYouWantToFormat.Replace(char.ConvertFromUtf32(13),"<br/>")
When you call Server.HtmlEncode
on the value you grab from a text box it'll look at the text and encode any HTML tags contained in that text box so for example <script></script>
would be encoded to <script></script>
.
来源:https://stackoverflow.com/questions/5421009/keep-formatting-entered-in-asp-net-textbox-carriage-return-new-line-etc