0

I have created a custom control with a collection property per the example on How do you build an ASP.NET custom control with a collection property?

When the control is added to a common ASP.Net aspx page it works as expected. However, when added to a Page Layout in Sharepoint the following error is thrown:

Unable to cast object of type 'System.Web.UI.CollectionBuilder' to type 'System.Collections.Generic.List`1[mytypes.mytype]'.

The code is pretty much identical to the code provided by the example shown in the link above. I do not think the fault lies in the control as it works fine in a plain web project.

Community
  • 1
  • 1
Windy
  • 13
  • 1
  • 4

1 Answers1

2

I dont think you can use generic lists in sharepoint. Use an ArrayList or customised List collection instead (use asp:ListItem as an exampe, it has its own collection type)

[ParseChildren(true, "Names")]
public class MyControl : Control {
    private List<PersonName> names;
    public MyControl() {
        names = new List<PersonName>();
    }
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    public List<PersonName> Names {
        get { return this.names; }
    }
}
public class PersonName {
    public string Name { get; set; }
}

UPDATE

Ahh i see the problem now, it is not to do with the generic list, it is because of the way you are doing the initialization.

  1. Create a private variable to hold the list private List<PersonName> names;
  2. Ensure that the property does not have a setter
djeeg
  • 6,685
  • 3
  • 25
  • 28
  • Thanks, but it doesn't have the desired effect. Unable to cast object of type 'System.Web.UI.CollectionBuilder' to type 'System.Collections.ArrayList' – Windy Jan 21 '11 at 08:17
  • Many thanks. Any ideas as to why it works if a setter is omitted? – Windy Jan 21 '11 at 11:45