Skip to main content
 首页 » 编程设计

c#之如何创建具有集合的控件

2024年02月23日31lvdongjie

好吧,让我更具体一点。我知道如何创建具有集合属性的控件,但我从未做过的是编写一个具有与之关联的标记的控件。

我想知道的是,给定一个这样定义的控件......

public class MyControl  
{ 
    public List<MySubItems> Items {get; set;} 
} 

我需要做什么才能创建标记,就像这样...

<MyStuff:MyControl runat="server" ID="MyControl1"> 
  <Items> 
    <MySubItem ... /> 
    <MySubItem ... /> 
  </Items> 
</MyStuff:MyControl> 

很抱歉,如果这看起来有点无聊,但我以前从未这样做过。

-- 斯图尔特

请您参考如下方法:

您需要申请 DesignerSerializationVisibilityAttribute连同 PersistenceModeAttribute在你的属性(property)上。假设它是一个 ASP.NET 控件。

这就是它的样子-

public class MyControl:WebControl 
{ 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
    PersistenceMode(PersistenceMode.InnerDefaultProperty)] 
    public List<MySubItems> Items {get; set;} 
} 

参见 MSDN: Web Control Collection Property Example获取详细示例。