【发布时间】:2021-09-01 18:56:54
【问题描述】:
我正在编写一个复合控件,它应该呈现放置在使用 aspx 页面中它的开始和结束标记之间的任何内容。
VB
Public Class MyComposite
Inherits CompositeControl
Implements INamingContainer
Public Property UserContentTemplate as ITemplate = Nothing
Public Overrides ReadOnly Property Controls() As ControlCollection
Get
EnsureChildControls()
Return MyBase.Controls
End Get
End Property
Protected Overrides Sub CreateChildControls()
' This is where I'm creating the controls
' for the composite
End Sub
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
' This is where I render the composite controls
End Sub
Protected Overrides Sub RecreateChildControls()
EnsureChildControls()
End Sub
Public Overrides Sub DataBind()
CreateChildControls()
ChildControlsCreated = True
MyBase.DataBind()
End Sub
End Class
从这里UserContentTemplate 在消费aspx页面中可用
<cc:MyComposite runat="server" ID="MyCompositeID">
<UserContentTemplate>
<asp:Button... />
<asp:TextBox... />
</UserContentTemplate>
</cc:MyComposite>
此时,asp:Button 和 asp:TextBox 没有被渲染。我已经查看了这个链接Building Templated Custom ASP.NET Serv Controls,但我不知道这是否适用或如何在我的情况下应用它。如果您查看链接,您会看到 <StatsTemplate> 标记内的 HTML 元素在自定义控件中呈现。
【问题讨论】:
-
在您的
MyComposite类中,您似乎没有使用UserContentTemplate属性。那是你的问题。 -
@Dai,这确实是问题所在。我不知道如何使用该属性。该属性现在所做的就是将
UserContentTemplate标记公开给aspx 页面上的消费者。那么你知道如何渲染UserContentTemplate标签中包含的内容吗?
标签: c# asp.net vb.net custom-server-controls