是否可以构建一个同时接受ListItem和ContentTemplate的UserControl?像这样:
<Custom:UserControl ID="ucTest" runat="server">
<Items>
<asp:ListItem Text="Test1" Value="1" />
<asp:ListItem Text="Test2" Value="2" />
</Items>
<ContentTemplate>
Here is some content!<br/>
<asp:Button ID="btnTest" runat="server" OnClick="SomeFunc" />
</ContentTemplate>
</Custom:UserControl>
答案 0 :(得分:2)
我将它们组合起来,似乎工作正常:
[ParseChildren(true), PersistChildren(false)]
public class Test : WebControl, INamingContainer
{
[ParseChildren(true, "Items")]
public class iTestItems
{
private ListItemCollection _Items;
[DefaultValue((string)null), MergableProperty(false), PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public virtual ListItemCollection Items
{
get
{
if (_Items == null)
_Items = new ListItemCollection();
return _Items;
}
}
}
private iTestItems _TestItems = null;
private ITemplate _ContentTemplate = null;
public event EventHandler TestClick = null;
[PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(iTestItems)),
TemplateInstance(TemplateInstance.Single)]
public iTestItems TestItems
{
get { return _TestItems; }
set { _TestItems = value; }
}
[PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(TemplateControl)),
TemplateInstance(TemplateInstance.Single)]
public ITemplate ContentTemplate
{
get { return _ContentTemplate; }
set { _ContentTemplate = value; }
}
}
用于:
<cc:Test ID="jqTestTest01" runat="server" OnTestClick="jqTestTest01_TestClick">
<TestItems>
<asp:ListItem Text="Tab One" Value="1" Selected="True" />
<asp:ListItem Text="Tab Two" Value="2" />
<asp:ListItem Text="Tab Three" Value="3" />
<asp:ListItem Text="Tab Four" Value="4" />
<asp:ListItem Text="Tab Five" Value="5" />
</TestItems>
<ContentTemplate>
<asp:Label ID="lblTestTest01" runat="server" Text="None" />
</ContentTemplate>
</cc:Test>
答案 1 :(得分:1)
此示例显示如何设置自定义控件以使多个子项符合您的需求:http://www.codeproject.com/KB/webforms/MikEllASPNetQuery.aspx。我发布了一个自定义控件示例,因为我非常确定您无法使用用户控件。
contenttemplate可以是带有getter / setter的ITemplate属性,而另一个可以利用ListItemCollection类。定义文章中定义的每个属性(这是一个包含多个内部对象引用的示例)。
HTH。