Custom User Control and Friendly Property Item Collection (Like ListBox and ListItems, but with List) on .ASPX

前端 未结 3 1623
野的像风
野的像风 2021-01-19 11:46

I have been wondering a long time how to do a public property for a User Control that work\'s like .NET native Item\'s collection Property (for Example, ListBox and ListItem

相关标签:
3条回答
  • 2021-01-19 12:24

    Just as an add on - the link above really helped. The only thing that I want to add is how you can directly register it in your website so that intellisense picks it up and without having a different assembly.

    Create the classes as described - instead if the rendering i override the OnInit method, since it is called earlier and I can play some more with the other prop. Create 2 namespaces:

    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<Slide>)
    {
     public class Slide{...}
     public class SlideCollectionEditor : CollectionEditor{...}
    }
    

    Now you can directly register them in your aspx pages or in the web config, depending on how often you will use the control.

    WEB CONFIG

    <add tagPrefix="ucSlideShow" tagName="SlideShow" src="~/x/Controls/Conference/SlideShow.ascx" />
    <add tagPrefix="ucSlideShow" namespace="x.Controls.Conference.SlideShowUC" assembly="WebAssembly" />
    

    PAGE

    <%@ Register TagPrefix="ucSlideShow" TagName="SlideShow" src="~/x/Controls/Conference/SlideShow.ascx"  %>
    <%@ Register TagPrefix="ucSlideShow" namespace="x.Controls.Conference" assembly="WebAssembly"  %>
    
    0 讨论(0)
  • 2021-01-19 12:47

    You can place a list of your type in the control class and decorate with PersistenceModeAttribute.

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

    An better example:

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

    0 讨论(0)
  • 2021-01-19 12:47

    You need to decorate the control and it's properties with sufficient information that the designer can pick up on details at design time. Have you checked this link?

    0 讨论(0)
提交回复
热议问题