从一个完整的html字符串构建HtmlGenericControl

Ken*_*ker 1 html c# asp.net

我希望能够将属性添加到html字符串,而无需构建解析器来处理html.在一个特定的情况下,我希望能够提取HTML的id或向html服务器端插入一个id.

说我有:

string stringofhtml = "<img src=\"someimage.png\" alt=\"the image\" />";
Run Code Online (Sandbox Code Playgroud)

我希望能够做到这样的事情:

HtmlGenericControl htmlcontrol = new HtmlGenericControl(stringofhtml);
htmlcontrol.Attributes["id'] = "newid";
Run Code Online (Sandbox Code Playgroud)

要么

int theid = htmlcontrol.Attributes["id"];
Run Code Online (Sandbox Code Playgroud)

这只是我可以访问/添加我拥有的html字符串属性的一种方式.

Jam*_*son 5

你可以这样做:

HtmlGenericControl ctrl = new HtmlGenericControl();
ctrl.InnerHtml = "<img src=\"someimage.png\" alt=\"the image\" />"; 
Run Code Online (Sandbox Code Playgroud)

您也可以使用LiteralControl,而不是HtmlGenericControl:

LiteralControl lit = new LiteralControl(stringOfHtml);
Run Code Online (Sandbox Code Playgroud)