-->

Custom User Control and Friendly Property Item Col

2019-02-23 17:23发布

问题:

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 ListItems):

<asp:ListBox blablabla>
    <asp:ListItem></asp:ListItem> <- Inline item collection...
</asp:ListBox>

I have been checking around the web but without any sucess. I think it must be any type of attribute that i need to add to the property, OR interface that should need to be inherited by the user control, but no clue about it, and have been thinking about it long time.

I have got to work it on a custom user control, but Visual Studio didn't recognised it as a valid item collection.

Let's say that we have this userControl:

public partial class userControls_Blablabla : System.Web.UI.UserControl
{
  public List<configItem> 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...
}

How should be done, to be able to do something like that on the .ASPX editor of Visual Studio, and it get recognised by Intellisense?

<User_Control:userControls_Blablabla ID="blablabla" ...blablabla....>
   <ConfigItem Name="1" Url="...." Color="..." />
   <ConfigItem Name="2" Url="...." Color="..." />
   <ConfigItem Name="3" Url="...." Color="..." />
</User_Control:userControls_Blablabla>

Sorry for my english, i know it's not very good.

Thanks in advance!

回答1:

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?



回答2:

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



回答3:

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"  %>