Asp.net 如何在网页中断线?

Asp.net 如何在网页中断线?,asp.net,sql-server,Asp.net,Sql Server,我想在显示到网页之前格式化内容。我正在数据库中将“\r\n\”存储为enter,并尝试在网页上显示内容之前替换它。服务器端我的代码是: lblComments.Text = Server.HtmlEncode(((DataRowView)e.Item.DataItem).Row["Comment"].ToString().Replace("\r\n", "<br>")); string comments = @"Comment Type: Public Comment: Comme

我想在显示到网页之前格式化内容。我正在数据库中将“\r\n\”存储为enter,并尝试在网页上显示内容之前替换它。服务器端我的代码是:

lblComments.Text = Server.HtmlEncode(((DataRowView)e.Item.DataItem).Row["Comment"].ToString().Replace("\r\n", "<br>"));
string comments = @"Comment Type: Public
Comment: Commented on the Data by user A";
lblComments.Text = comments.Replace("\n","<br>");
但是,它在一行中显示所有内容。

您需要使用

.Replace("\r\n", "<br/>")
。替换(“\r\n”和“
”)

并且只在Server.HtmlEncode之后,因为您不希望

本身被编码为
br/
(原始),按字面意思显示为

您可以将输出包装在
..
元素中,而不是将换行符替换为

。这样,换行符应该是守恒的

<pre><asp:Label id="lblComments" runat="server" /></pre>
例如,在标签周围放置
元素:

Replace("\n", "<br>")

试试看

替换(“\n”和“
”)
这个测试没有编码

string comments=@“注释类型:Public
备注:用户A对数据进行了备注”;
lblComments.Text=注释。替换(“\n”和“
”);
实际输出是什么?查看呈现页面的源代码时,您是否看到

元素,或者是否获得
\r\n
?请注意
\r\n
,这是仅在Windows上的行终止符,您确定永远不必处理unix/mac格式的文件吗?如果

是HTML编码的,我希望看到

呈现在页面上。这不是OP发布的内容。
string comments = @"Comment Type: Public
Comment: Commented on the Data by user A";
lblComments.Text = comments.Replace("\n","<br>");