在aspx上渲染HTML字符串 [英] Render HTML string on aspx

查看:91
本文介绍了在aspx上渲染HTML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我被这个问题困扰.我正在从数据库中获取HTML字符串,并希望在aspx页面上的面板中显示它.

我的字符串就像< html> <身体> HTML代码</body> </html> .

我想在aspx上的窗口/面板中显示此HTML

有人可以提出任何建议吗?任何帮助将不胜感激.

谢谢,

Hello folks ,

I am stuck with this issue. I am fetching a HTML string from database and I want to display it in a panel on aspx page.

My string is like <html> <body> HTML Code</body> </html> .

I want to display this HTML in a window/ panel on aspx

Can someone please suggest any way ? Any help would be appreciated .

Thanks,

推荐答案

由于我不知道您遇到了什么困难,我只是假设您遇到了最常见的问题之一:HTML实体的编码.如果是这样,请参阅:
http://msdn.microsoft.com/en-us/library/73z22y6h.aspx [ ^ ].

参见类System.Web.HttpUtility:
http://msdn.microsoft.com/en-us/library/system.web. httputility.aspx [ ^ ].

如果您还有其他问题,请不要怪我:您没有告诉我们这些问题.欢迎您提出其他问题.



也许iframe并不是您所需要的,因为它的内容在HTML文件外部,是从外部文件加载的.您将需要使用从数据库中获取的内容创建一个单独的HTML文件,使用属性src生成iframe元素,并将属性的值设置为所生成文件的名称.这太丑了.

您最好使用任何div之类的块元素,并将其属性innerHTML设置为从数据库中获取的内容.但是,它不能是整个HTML文件,只能是在div元素下有效的HTML片段.

如果两种解决方案都不适合您(可能会发生),则应检查设计.毕竟,您的要求是不要使用iframe或某些特定的东西.您只需要动态应用某些渲染即可.

—SA
As I have not idea what is your difficulties, I just assumed you have one of the most common problem: encoding of HTML entities. If so, please see:
http://msdn.microsoft.com/en-us/library/73z22y6h.aspx[^].

See the class System.Web.HttpUtility:
http://msdn.microsoft.com/en-us/library/system.web.httputility.aspx[^].

If you have other problems, please don''t blame me: you did not tell us about them. You are welcome to ask other questions.



Perhaps iframe is not what you need, because its content is external to your HTML file, is loaded from an external file. You would need to create a separate HTML file with the content taken from your database, generate iframe element with the attribute src, set the attribute''s value to the name of the generated file. This would be too ugly.

You should better use any block element like div and set its attribute innerHTML to the content taken from your database. It cannot be the whole HTML file though, only the HTML fragment which is valid under the div element.

If both solutions are not good enough for you (which can happen), you should review your design. After all, your requirement is not to use iframe or something certain. You just need certain rendering applied dynamically.

—SA


step 1: make the panel control runat="server"
step 2:remove html and body tag from your string
step 3:using panel id in .cs file id.controls.add(new LiteralControl(your string))



如果要在iframe中显示,请使用id.innerHtml =您的字符串



and if you want to display in iframe then use id.innerHtml=your string


这篇关于在aspx上渲染HTML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆