简体   繁体   English

如何从数据库渲染gridview asp.net上的html标签

[英]how to render html tags on gridview asp.net from database

I have asp.net grid view to display techs notes. 我有asp.net网格视图以显示技术说明。 I am trying to force the grid view to display it as user enterd in text area 我试图强制网格视图在用户在文本区域中输入时显示它

note: the grid view display the note in one line without break line or orderlist 注意:网格视图在一行中显示注释,没有换行符或订单列表

example user Enter look like this 示例用户Enter看起来像这样

-hello World 1 -hello World 1

-hello World 2 -hello World 2

-hello World 3 -hello World 3

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor坐下,一直保持着安静的状态。 Quisque gravida nibh mauris. Quisque gravida nibh mauris。 Nam sit amet orci quis justo lacinia dictum nec vitae elit. Nam sit amet Orci quis justo lacinia dictum nec vitae elit。 Integer id nulla vitae nunc aliquam fringilla. 整数id nulla vita nunc aliquam fringilla。 Donec et nibh et odio ultrices cursus et quis lectus. Donec et nibh et dioo ultrices cursus et quis lectus。 Vestibulum ac tellus sit amet elit ullamcorper adipiscing. Vestibulum actellus坐在alit elit ullamcorper辅助。 Quisque eu nisi eros. Quisque eu nisi eros。 Integer dignissim, mi id tempus cursus, nisl dui euismod massa, adipiscing tincidunt dolor turpis ac magna. 整数dignissim,mi tempus cursus,nisl dui euismod massa,忠告Tincidunt dolor turpis ac magna。 Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; 紫茎泽兰(Faucibus orci luctus et ultrices cuuelia Cubilia Curae)中的初生初生植物;

You can try with this code 您可以尝试使用此代码

var input = "-hello World 1 -hello World 2";
var result = input.Replace("-",Environment.NewLine + "-" );

Its because when you entered the data in text area. 这是因为当您在文本区域中输入数据时。 It generates \\n or \\r for new line. 它为新行生成\\ n或\\ r。 And in terms of html it doesn't break the line. 而且就html而言,它不会打破常规。 So the solution is replace these characters with 所以解决方案是将这些字符替换为
tag. 标签。

for example in the bind expression use this : 例如在bind表达式中使用this:

<%# Convert.ToString(Eval("Content")).Replace("\n","<br />").Replace("\r","<br />") %>

Hope this will help! 希望这会有所帮助!

I don't know of any ready made way to do this. 我不知道有什么现成的方法可以做到这一点。 So just convert newlines to <br> tags and make other needed changes manually. 因此,只需将换行符转换为<br>标记,然后手动进行其他所需的更改即可。

Or you can do something a little more fancy by converting newlines into new HTML paragraphs like I did in my article Converting Text to HTML . 或者,您可以像在我的文章《 将文本转换为HTML》中所做的那样,通过将换行符转换为新的HTML段落来做更多的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM