如何将我的属性从ExpandedHomeSlider类绑定到SPListItemCollection?

时间:2014-02-07 18:32:28

标签: c# .net properties sharepoint-2010 user-controls

我创建了公共类HeroImage和ExpandedHomeSlider来访问将提供给Repeater的自定义列表,以便轻松修改SPListItem。 如何将我的属性从ExpandedHomeSlider类绑定到SPListItemCollection?我是SP开发的新手。任何帮助表示赞赏:]。

public partial class HomePageHeroRotator : UserControl
{
    public SPListItem SpListItem;

    public class HeroImage
    {
        public SPListItem SpListItem;
        public string heroImageUrl
        {
            get { return SpListItem["HeroImage"].ToString(); }
        }
        public string heroImageUrl2
        {
            get { return SpListItem["HeroImageCallOut"].ToString(); }
        }
    }
    public class ExpandedHomeSlider 
    {
        public SPListItem SpListItem;
        public string explanatoryText
        {
            get { return SpListItem["ExplanatoryText"].ToString(); }
        }
        public string attentionExplanatoryText2
        {
            get { return SpListItem["ExplanatoryText"].ToString(); }
        }
        public string explanatoryText2
        {
            get { return SpListItem["ExplanatoryText"].ToString(); }
        }
        public string homeSliderImageUrl
        {
            get { return SpListItem["ExpandedSliderImage"].ToString(); }
        }
        public string GreenFlyoutText
        {
            get { return SpListItem["GreenFlyoutText"].ToString(); }
        } 
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        //Query List for all List Items
        //ASCX to render list items
        SPSite site = null;
        if (SPContext.Current != null)
        {
            site = SPContext.Current.Site;
        }
        SPList list = site.RootWeb.Lists["HomePageHero"];

        SPListItemCollection myItems = list.Items;
        IEnumerable eList = myItems.Fields as IEnumerable;

        //foreach loop 

        HomePageHeroRpt.DataSource = eList;
        HomePageHeroRpt.DataBind();

}

我的Schema.xml列表包含以下字段:

        <ViewFields>
      <FieldRef Name="LinkTitle" />
      <FieldRef Name="ExplanatoryText" />
      <FieldRef Name="GreenFlyoutText" />
      <FieldRef Name="BrownFlyoutText" />
      <FieldRef Name="RedFlyoutText" />
      <FieldRef Name="OrangeFlyoutText" />
      <FieldRef Name="LightBlueFlyoutText" />
      <FieldRef Name="BlueFlyoutText" />
      <FieldRef Name="AttentionExplanatoryText" />
      <FieldRef Name="ExplanatoryText2" />
      <FieldRef Name="HeroImage" />
      <FieldRef Name="HeroImageCallOut" />
      <FieldRef Name="ExpandedSliderImage" />
    </ViewFields>

1 个答案:

答案 0 :(得分:0)

首先,您需要做的是将SPListItem个对象的集合转换为自定义对象的集合。 Select是一个可以为您执行此操作的LINQ方法。然后你可以将它绑定到你的转发器:

HomePageHeroRpt.DataSource = list.Items.Cast<SPListItem>()
    .Select(item => new ExpandedHomeSlider(){SpListItem = item});

在网站上注意,有一个名为“SPMetal”的工具能够为您生成这样的类,因此您无需自己创建它们。 (他们有相当多的设计差异,但每个领域拥有一个属性的总体目标是相同的。)