【问题标题】:Adding custom control with a collection property to a SharePoint Page Layout将具有集合属性的自定义控件添加到 SharePoint 页面布局
【发布时间】:2011-01-20 12:49:37
【问题描述】:

我根据How do you build an ASP.NET custom control with a collection property? 上的示例创建了一个带有集合属性的自定义控件

当控件被添加到一个常见的 ASP.Net aspx 页面时,它会按预期工作。但是,当添加到 Sharepoint 中的页面布局时,会引发以下错误:

无法将“System.Web.UI.CollectionBuilder”类型的对象转换为“System.Collections.Generic.List`1[mytypes.mytype]”类型。

该代码与上面链接中显示的示例提供的代码几乎相同。我认为问题不在于控件,因为它在普通的 Web 项目中运行良好。

【问题讨论】:

    标签: sharepoint custom-controls


    【解决方案1】:

    我认为您不能在 sharepoint 中使用通用列表。改用 ArrayList 或自定义 List 集合(以 asp:ListItem 为例,它有自己的集合类型)

    [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; }
    }
    

    更新

    啊,我现在看到了问题,这与通用列表无关,这是因为您进行初始化的方式。

    1. 创建一个私有变量来保存列表private List&lt;PersonName&gt; names;
    2. 确保该属性没有设置器

    【讨论】:

    • 谢谢,但效果并不理想。无法将“System.Web.UI.CollectionBuilder”类型的对象转换为“System.Collections.ArrayList”
    • 非常感谢。如果省略了 setter,为什么它会起作用?
    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2019-10-29
    • 2011-06-27
    相关资源
    最近更新 更多