我需要开发一个类似模板的用户控件,它可以接受任何任意内容,包括其他控件。
WmlCard.ascx
<asp:PlaceHolder ID="phContent" runat="server">
<card id="<%= Me.CardId %>" title="<%= Me.Title %>">
<p>
<%= Me.InnerText %>
</p>
</card>
</asp:PlaceHolder>
它将以这种方式使用:
ListaTelefonica.aspx
<%@ Register TagPrefix="sic" TagName="Card" Src="~/path/to/WmlCard.ascx"%>
<sic:Card ID="BuscaCard" runat="server" CardId="busca" title="Lista Telefônica" Visible="false">
<asp:Literal ID="SomeLiteral" runat="server" Visible="false">Some text<br /></asp:Literal>
<asp:Literal ID="RuntimeFilledLiteral" runat="server" Visible="false" /><br /></asp:Literal>
Any text.
</sic:Card>
不幸的是 Ascx 用户控件必须继承System.Web.UI.UserControl
. 例如,如果我可以继承System.Web.UI.WebControls.WebControl
,那将是小菜一碟。
我的问题与此类似,但控件应接受其中的任何其他控件,而不仅仅是文本。
我尝试<ParseChildren(False)>
并覆盖AddParsedSubObject(Object)
in WmlCard
,但这不是解决方案,因为 html 是在页面加载之前呈现的RuntimeFilledLiteral.Text
,例如,更改页面的 Page_Load 中的值毫无意义。
我得到的第一个错误:
解析器错误消息:类型“ASP.sic_sicphone_usercontrol_wmlcard_ascx”没有名为“Literal”的公共属性。
添加后<PersistenceMode(PersistenceMode.InnerDefaultProperty), ParseChildren(True, "InnerText")>
:
“sic:Card”的“InnerText”属性不允许子对象。
与上述相同,但将 WmlCard 的属性InnerText
类型从更改String
为List(Of Object)
:
'System.Collections.IList' 中不允许文字内容('Any text.')。
添加<ParseChildren(False)>
和覆盖后AddParsedSubObject
:
没有错误消息,但在我有机会更改其内部控件运行时属性之前,WmlCard 的内容已呈现为 html。
如果我将 WmlCard 更改为继承System.Web.UI.WebControls.PlaceHolder
而不是System.Web.UI.UserControl
:
此处不允许使用“...UserControl.WmlCard”,因为它不扩展类“System.Web.UI.UserControl”。