如何使用C#来绑定网站地图数据源树视图 [英] How to Bind SiteMap Treeview datasource using c#
本文介绍了如何使用C#来绑定网站地图数据源树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有地图数据源控制我的网页上
I have SiteMap Datasource control on my page
<div id="content_inside">
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" ExpandDepth="2"
</asp:TreeView>
</div>
没有我想绑定它的使用低于code段树视图
No I am trying to bind it's tree view using the below code snippet
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'是从数据库
Where 'GetAll' is the function which fetched data from the database
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;
}
当我渲染,出现以下错误
when I render, the following error occurred
HierarchicalDataBoundControl only accepts data sources that implement IHierarchicalDataSource or IHierarchicalEnumerable.
任何建议?
推荐答案
您需要执行 IHierarchicalEnumerable
接口。
您可以做到这一点如下:
You can do this as follows:
public class MovieCollection : List<Movie>, IHierarchicalEnumerable
{
public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return enumeratedItem as IHierarchyData;
}
public System.Collections.IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
添加您现有的电影是这样的:
Then add your existing movies like this:
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;
}
树视图通常用于显示虽然嵌套数据和当前的数据只包含一个级别。
The TreeView is usually used to display nested data though and your current data only contains one level.
这篇关于如何使用C#来绑定网站地图数据源树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文