如何从纯文本创建WebControl

时间:2014-06-04 09:39:21

标签: c# asp.net code-behind

我有一个TableCell“cell”,其中“cell.Text”包含类似

的内容
"<input type=\"checkbox\" tabindex=\"-1\" style=\"height:17px\" checked=\"checked\" />".

目标是删除样式属性“height”!

PLSE。请注意,“style”属性可以包含一些其他属性(如“backgroundColor”等) 并且可以在“height:”和值(此处为“17px”)之间使用whitspace 并且它可以(但不得)以“;”结束 等...... 所以我解析它是没有意义的(使用“.Substring(...)”等。)。

另一种可能的方法是创建一个WebControl并使用“.Remove(...)”。

好主意,但没有工作:

    var control = Page.ParseControl(cell.Text);
    var tmpPage = new Page();
    tmpPage.Controls.Add(control);

    var webControl = tmpPage.Controls[0] as WebControl; // this return null because "Control" could not be casted to WebControl

    var before = webControl.Style;
    webControl.Style.Remove(HtmlTextWriterStyle.Height);
    var after = webControl.Style;

但这不起作用。看我的评论。

如何实现目标?

提前谢谢。

0 个答案:

没有答案