我的页面上有SiteMap数据源控件
<div id="content_inside">
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" ExpandDepth="2"
</asp:TreeView>
</div>
不,我正在尝试使用以下代码片段绑定它的树视图
protected void Page_Load(object sender, EventArgs e)
{
var context = new moviescontext ();
var movies= context.movie.GetAll(ref context);
if (!IsPostBack)
{
TreeView1.DataSourceID = "SiteMapDataSource1";
TreeView1.DataSource = movies;
TreeView1.DataBind();
}
}
其中'GetAll'是从数据库中提取数据的函数
public static List<movies> GetAll(this IEnumerable<movie> movie, ref moviescontext ctx)
{
var movies= ctx.movie
.Select(s => new movies
{
MovieId= s.MovieID,
MovieName = s.MovieName,
}).ToList();
return movies;
}
渲染时,发生以下错误
HierarchicalDataBoundControl only accepts data sources that implement IHierarchicalDataSource or IHierarchicalEnumerable.
有什么建议吗?
答案 0 :(得分:1)
您需要实现IHierarchicalEnumerable
界面。
您可以按照以下方式执行此操作:
public class MovieCollection : List<Movie>, IHierarchicalEnumerable
{
public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return enumeratedItem as IHierarchyData;
}
public System.Collections.IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
然后添加您现有的电影:
public static List<movies> GetAll(this IEnumerable<movie> movie, ref moviescontext ctx)
{
var movies= ctx.movie
.Select(s => new movies
{
MovieId= s.MovieID,
MovieName = s.MovieName,
}).ToList();
var movieCollection = new MovieCollection();
movieCollection.AddRange(movies);
return movieCollection;
}
TreeView通常用于显示嵌套数据,而您当前的数据只包含一个级别。