我遇到了下一种情况:在ITemplate对象中,我正在渲染一个包含<li>
元素的Repeater,通常是这样做的:
Dim literalControl as new LiteralControl
literalControl = "<li>blah blah</li>"
container.Controls.Add(literalControl)
但在某一点上,我需要添加另一个函数返回的PlaceHolder,该函数用于其他目的,而不仅仅是这个中继器,我得到了这样的东西:
Dim literalControl as new LiteralControl
Dim plTable As PlaceHolder
literalControl = "<li>blah blah</li>"
MyObject.Render(AComplexObect, plTable)
container.Controls.Add(literalControl)
container.Controls.Add(plTable)
这很好,但placehoder中的元素保持在<li>
标记之外,我希望它在里面。plTable
内部有许多不同类型的元素,所以我循环将其内容转换为Literal或LiteralControl会抛出错误,因为并非所有元素都可以转换为这些类型。
我有什么选择?
感谢
这对你有用吗:
Dim literalControl As New LiteralControl
Dim plTable As PlaceHolder
Dim swWriter As New StringWriter
Dim htwWriter As New HtmlTextWriter(swWriter)
MyObject.Render(AComplexObect, plTable)
MyObject.RenderControl(htwWriter)
literalControl.Text = "<li>" & swWriter.ToString() & "</li>"
container.Controls.Add(literalControl)