ASP.NET解析原始HTML到控件 [英] ASP.NET Parsing raw HTML into Controls

查看:176
本文介绍了ASP.NET解析原始HTML到控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在ASP.NET中服用含有一些HTML字符串,使ASP.NET来分析它,并为我控制?例如:

 字符串rawHTML =<表>< TD>< TD>细胞< / TD>< / TR>< /表>,
HTMLTABLE表= MagicClass.ParseTable(rawHTML);

我知道,这是一个坏的事情,但我在不幸的情况,这是真的,我可以实现我需要什么(我不能修改这个特别的同事的code)的唯一途径。

另外,我知道LiteralControl可以让你有在它任意的HTML控件,但不幸的是我需要让他们转换成适当的控制。

不幸的是,HTMLTABLE不支持InnerHTML属性。我需要preserve的HTML树,正是因为它是,所以我不能把它变成一个< D​​IV> 标签

感谢。


解决方案

我想你会得到最接近的是的 Page.ParseControl ,这是ASP.NET分析器。缺点是文本你的的一个LiteralControl,因为它没有它=服务器 - 所以你会做的字符串操作事先非常点点

在otherwords:

<$p$p><$c$c>this.ParseControl(\"<table><tr><td>Cell</td></tr></table>\")

会产生:

 控制
 LiteralControl

,而

  this.ParseControl(&LT;表RUNAT = \\服务器\\&GT;&LT; TR&GT;&LT; TD&GT;细胞&LT; / TD&GT;&LT; / TR&GT;&LT; /表&gt;)

会产生:

 控制
 HTMLTABLE
  HtmlTableRow
   HtmlTableCell
    LiteralControl

Is it possible in ASP.NET to take a string containing some HTML and make ASP.NET to parse it and create a Control for me? For example:

string rawHTML = "<table><td><td>Cell</td></tr></table>";
HTMLTable table = MagicClass.ParseTable(rawHTML);

I know that this is a bad thing to do but I am in the unfortunate situation that this is really the only way I can achieve what I need (as I cannot modify this particular coworker's code).

Also, I know that LiteralControl allows you to have a control with arbitrary HTML in it, but unfortunately I need to have them converted to proper control.

Unfortunately, HTMLTable does not support the InnerHTML property. I need to preserve the HTML tree exactly as it is, so I cannot put it into a <div> tag.

Thanks.

解决方案

The closest I think you'll get is Page.ParseControl, which is the ASP.NET parser. The downside is that the text you have is a LiteralControl, since it doesn't have runat="server" on it - so you 'll do a very tiny bit of string manipulation beforehand.

In otherwords:

this.ParseControl("<table><tr><td>Cell</td></tr></table>")

will produce:

Control
 LiteralControl

whereas:

this.ParseControl("<table runat=\"server\"><tr><td>Cell</td></tr></table>")

will produce:

Control
 HtmlTable
  HtmlTableRow
   HtmlTableCell
    LiteralControl

这篇关于ASP.NET解析原始HTML到控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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