HTMLen code HTMLde codeHTMLen、code、HTMLde

2023-09-06 08:42:48 作者:拿铁三分糖

我有一个文本区域,我想以存储用户的HTML格式的类似段落分隔符,编号列表中输入数据库中的文本。我使用HTMLen code和HTMLde code这一点。

I have a text area and I want to store the text entered by user in database with html formatting like paragraph break, numbered list. I am using HTMLencode and HTMLdecode for this.

我的code样品是这样的:

Sample of my code is like this:

string str1 = Server.HtmlEncode(TextBox1.Text);
Response.Write(Server.HtmlDecode(str1));

如果有2个段落用户输入的文本,STR1显示字符\段落的R \ñ\ r \ñ。但是当它写入到屏幕上,只需追加与第一第二段落。虽然我对其进行解码,为什么没有打印2款?

If user entered text with 2 paragraphs, str1 shows characters \r\n\r\n between paragraphs. but when it writes it to screen, just append 2nd paragraph with 1st. While I'm decoding it, why doesn't it print 2 paragraphs?

推荐答案

简单的解决办法是这样:

The simple solution would be to do:

string str1 = Server.HtmlEncode(TextBox1.Text).Replace("\r\n", "<br />");

这是假设你只关心获得正确的&LT; BR /&GT; 标签的地方。如果你想有一个真正的格式,你需要像Aaronaught图书馆建议。

This is assuming that you only care about getting the right <br /> tags in place. If you want a real formatter you will need a library like Aaronaught suggested.

 
精彩推荐
图片推荐