ListVew绑定Linq匿名类型查询结果,结合DataPager分页后的错误:ListView 具有的数据源必须实现 ICollection
1、用Linq连接三个dataset : ArticleSpeakerDataSet , ArticleSpeakerInfoDataSet , ArticleDurationDataSetvar Lv3MainList = from main in ArticleDataSet.Tables[0].AsEnumerable()
join ExtSpeaker in ArticleSpeakerDataSet.Tables[0].AsEnumerable() on main.Field<string>("ArticleGUID") equals ExtSpeaker.Field<string>("articleguid")
join ExtSpeakerInfo in ArticleSpeakerInfoDataSet.Tables[0].AsEnumerable() on main.Field<string>("ArticleGUID") equals ExtSpeakerInfo.Field<string>("articleguid")
join ExtDuration in ArticleDurationDataSet.Tables[0].AsEnumerable() on main.Field<string>("ArticleGUID") equals ExtDuration.Field<string>("articleguid")
select new
{
ArticleGUID = main.Field<string>("ArticleGUID"),
Title = main.Field<string>("Title"),
CreateTime = main.Field<DateTime>("CreateTime"),
Filename = main.Field<string>("Filename"),
Speaker = ExtSpeaker.Field<string>("PropertyValue"),
SpeakerInfo = ExtSpeakerInfo.Field<string>("PropertyValue"),
Duration = ExtDuration.Field<string>("PropertyValue")
};
2、绑定Listview:
Lv3MainListItems.DataSource = Lv3MainList;
Lv3MainListItems.DataBind();
3、ListView和DataPager
<table class="SearchResultContentTable">
<asp:ListView ID="Lv3MainListItems" runat="server">
<ItemTemplate>
<tr>
<td>
<a href=""><%# Eval("Title") %></a>
</td>
<td>
<%# Eval("Speaker") %>
</td>
<td>
<%# Eval("Duration")%>
</td>
<td>
<%# Eval("CreateTime")%>
</td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<table style="">
<tr>
<td>
未返回数据。
</td>
</tr>
</table>
</EmptyDataTemplate>
<LayoutTemplate>
<tbody>
<asp:Panel ID="itemPlaceholder" runat="server">
</asp:Panel>
</tbody>
</LayoutTemplate>
</asp:ListView>
</table>
<asp:DataPager runat="server" ID="Lv3MainListItemsPager"
PagedControlID="Lv3MainListItems">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true"
FirstPageImageUrl="" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
4、报错详细信息
“/”应用程序中的服务器错误。
ID 为“Lv3MainListItems”的 ListView 具有的数据源必须实现 ICollection,或在 AllowPaging 为 true 的情况下可执行数据源分页。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: ID 为“Lv3MainListItems”的 ListView 具有的数据源必须实现 ICollection,或在 AllowPaging 为 true 的情况下可执行数据源分页。
源错误:
行 41: IQueryable Lv3MainListQueryable = Lv3MainList.AsQueryable();
行 42: Lv3MainListItems.DataSource = Lv3MainListQueryable;
行 43: Lv3MainListItems.DataBind();
行 44: }
行 45:
源文件: D:\Work\VS\colleges\level3.aspx.cs 行: 43
堆栈跟踪:
[InvalidOperationException: ID 为“Lv3MainListItems”的 ListView 具有的数据源必须实现 ICollection,或在 AllowPaging 为 true 的情况下可执行数据源分页。]
System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +439433
System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data) +33
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
System.Web.UI.WebControls.ListView.PerformSelect() +57
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
colleges.level3.Page_Load(Object sender, EventArgs e) in D:\Work\VS\colleges\level3.aspx.cs:43
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
版本信息: Microsoft .NET Framework 版本:2.0.50727.5472; ASP.NET 版本:2.0.50727.7031
请问各位高手这个问题应该怎么解决?
--------------------编程问答-------------------- Lv3MainList =Lv3MainList.ToList();
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/ --------------------编程问答-------------------- 感谢楼上解答
不过现在有遇到了一个问题,就是成功显示分页后,前后页按钮,页面数连接都不起作用了,我加了
protected void Lv3MainListItems_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
this.Lv3MainListItemsPager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
Lv3MainListItems.DataBind();
}
之后,页面编号起作用,前后页按钮还是不起作用,不知道该怎么解决...
另外不知道为何必须在aspx页面前加EnableEventValidation="false",不然也会报错。。。 --------------------编程问答-------------------- 我是把数据查询和绑定的代码放在Page_Load里
是不是应该放在PreRender里? --------------------编程问答--------------------
数据库查询应该放在你的数据库访问层
绑定的代码你根据需要放在页面的cs文件里
你那错误 原因是因为你查询出来的var Lv3MainList 是一个IEnumerable 而 IEnumerable
没有实现ICollection 接口,所以要把Lv3MainList tolist();toList后,就是一个实现了IList列表的集合
而IList实现了ICollection 接口
所以不错错了!
var Lv3MainList = (from main in ArticleDataSet.Tables[0].AsEnumerable()
join ExtSpeaker in ArticleSpeakerDataSet.Tables[0].AsEnumerable() on main.Field<string>("ArticleGUID") equals ExtSpeaker.Field<string>("articleguid")
join ExtSpeakerInfo in ArticleSpeakerInfoDataSet.Tables[0].AsEnumerable() on main.Field<string>("ArticleGUID") equals ExtSpeakerInfo.Field<string>("articleguid")
join ExtDuration in ArticleDurationDataSet.Tables[0].AsEnumerable() on main.Field<string>("ArticleGUID") equals ExtDuration.Field<string>("articleguid")
select new
{
ArticleGUID = main.Field<string>("ArticleGUID"),
Title = main.Field<string>("Title"),
CreateTime = main.Field<DateTime>("CreateTime"),
Filename = main.Field<string>("Filename"),
Speaker = ExtSpeaker.Field<string>("PropertyValue"),
SpeakerInfo = ExtSpeakerInfo.Field<string>("PropertyValue"),
Duration = ExtDuration.Field<string>("PropertyValue")
}).ToList();
Lv3MainListItems.DataSource = Lv3MainList;
Lv3MainListItems.DataBind();
这样也不会报错了 --------------------编程问答-------------------- 感谢楼上前辈解答
前辈说的 数据库访问层 是否是用一个单独的类存放查询方法?
还是不知道dataPager 页数字 和 翻页按钮 为什么不起作用。。。?
补充:.NET技术 , ASP.NET