Sử dụng ParseChildrenAttribute và PersistChildrenAttribute thuộc tính:
[ParseChildren(false)]
[PersistChildren(true)]
public class MyControl : UserControl { }
Điều này sẽ gây ra bất kỳ điều khiển bạn đặt bên trong các tài liệu tham khảo:
<uc:MyControl runat="server">
<asp:TextBox runat="server" />
<uc:MyControl>
Để được nối vào cuối của bộ sưu tập Controls nội dung UserControl của bạn .
Tuy nhiên, nếu bạn muốn có bộ sưu tập các điều khiển, có thể bạn nên sử dụng điều khiển máy chủ chứ không phải điều khiển người dùng. Đối với kiểm soát hoạt động như sau:
<foo:TabControl runat="server">
<Tabs>
<foo:Tab CssClass="myclass" Title="Hello World" />
</Tabs>
</foo:TabControl>
Bạn cần lớp Điều khiển có thuộc tính Tab; thuộc tính Tab phải là Bộ sưu tập; và nó phải chứa các đối tượng kiểu Tab. Tôi đã tạo ra ba lớp học ở đây:
[ParseChildren(true, "Tabs")]
public class TabControl: WebControl, INamingContainer
{
private TabCollection _tabs;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public TabCollection Tabs
{
get
{
if (_tabs == null)
{
_tabs = new TabCollection();
}
return _tabs;
}
}
protected override void Render(HtmlTextWriter writer)
{
foreach (Tab tab in Tabs)
{
writer.WriteBeginTag("div");
writer.WriteAttribute("class", tab.CssClass);
writer.Write(HtmlTextWriter.TagRightChar);
writer.Write("this is a tab called " + tab.Title);
writer.WriteEndTag("div");
}
}
}
Và lớp tab:
public class Tab
{
public string CssClass { get; set; }
public string Title { get; set; }
}
Và bộ sưu tập tab:
public class TabCollection : Collection<Tab> { }
Nguồn
2009-03-03 00:19:49
Tôi sẽ cung cấp tính năng này. Tôi đang sử dụng một điều khiển (máy chủ?) Tùy chỉnh ngay bây giờ; Tôi nghĩ rằng "uc" có thể khiến bạn tin rằng đó là điều khiển của người dùng. – rball