.ASPX上的自定义用户控件和友好属性项集合(如ListBox和ListItems,但列表)

我一直想知道如何为用户控件执行公共属性,其工作方式类似于.NET native Item的集合属性(例如,ListBox和ListItems):

  <- Inline item collection...  

我一直在网上查看,但没有任何成功。 我认为它必须是我需要添加到属性的任何类型的属性,OR接口应该需要由用户控件inheritance,但是没有关于它的线索,并且一直在考虑它。

我必须在自定义用户控件上工作,但Visual Studio没有将其识别为有效的项集合。

假设我们有这个userControl:

 public partial class userControls_Blablabla : System.Web.UI.UserControl { public List ConfigItem {get; set; } blablabla...rest logic here... } public class configItem { public string Name {get; set;} public string Url {get; set;} public string Color {get; set;} blablabla...rest logic here... } 

应该如何做,能够在Visual Studio的.ASPX编辑器上做类似的事情,并得到Intellisense的识别?

      

对不起我的英语,我知道这不是很好。

提前致谢!

您需要使用足够的信息来装饰控件及其属性,以便设计人员可以在设计时获取详细信息。 你看过这个链接了吗?

您可以在控件类中放置类型列表,并使用PersistenceModeAttribute进行装饰。

 [PersistenceMode(PersistenceMode.InnerProperty)] public List ConfigItem { get; set; } 

一个更好的例子:

http://am-blog.no-ip.org/BlogEngine/post/2010/04/13/ASP-NET-Custom-Control-with-PersistenceModeInnerProperty-using-Server-Controls.aspx

就像添加一样 – 上面的链接确实有帮助。 我想要添加的唯一内容是如何直接在您的网站上注册它,以便intellisense选择它并且没有不同的程序集。

如上所述创建类 – 相反,如果渲染我重写OnInit方法,因为它之前被调用,我可以与其他prop一起玩更多。 创建2个名称空间:

 namespace x.Controls.Conference - add the class that derives from UserControl { public partial class SlideShow : System.Web.UI.UserControl{...} } namespace x.Controls.Conference.SlideShowUC - add here the base class of the collection item in the UC (Collection) { public class Slide{...} public class SlideCollectionEditor : CollectionEditor{...} } 

现在,您可以直接在aspx页面或Web配置中注册它们,具体取决于您使用控件的频率。

WEB CONFIG

   

 <%@ Register TagPrefix="ucSlideShow" TagName="SlideShow" src="~/x/Controls/Conference/SlideShow.ascx" %> <%@ Register TagPrefix="ucSlideShow" namespace="x.Controls.Conference" assembly="WebAssembly" %>