使用 LinqDataSource 和 linq to sharepoint 进行分页
我正在开发 Sharepoint 2010 可视化 Web 部件,并尝试在其中使用 LinqDataSource 来处理 GridView 中的分页和排序。 我用 spmetal 制作了数据上下文和实体对象。现在这是我的代码:
我的标记:
<%@ Register TagPrefix="asp" Namespace="System.Web.UI.WebControls" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<asp:LinqDataSource runat="server" ID="LinqDataSource1" OnSelecting="MySelecting" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="3"
AutoGenerateColumns="False" DataSourceID="LinqDataSource1"
EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="title" HeaderText="Title" />
</Columns>
</asp:GridView>
和我的代码:
protected void MySelecting(object sender, LinqDataSourceSelectEventArgs e)
{
TestEntitiesDataContext dc = new TestEntitiesDataContext("http://sp/sites/test");
e.Result = from item in dc.TestList
select new
{
title = item.Title,
numberField = item.NumberField.ToString()
};
}
现在的问题是,当我尝试查看网站上的 Web 部件时,出现此错误: 类型“System.Int32”的表达式不能用于返回类型“System.Object”
当我在网格视图上停用分页时,此错误消失。
您知道为什么会发生这种情况吗?
我将不胜感激任何帮助。
I am developing a a Sharepoint 2010 visual webpart and I am trying to use LinqDataSource in it to handle paging and sorting in a GridView.
I made my datacontext and entity objects with spmetal. and now this is my code:
my mark up:
<%@ Register TagPrefix="asp" Namespace="System.Web.UI.WebControls" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<asp:LinqDataSource runat="server" ID="LinqDataSource1" OnSelecting="MySelecting" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="3"
AutoGenerateColumns="False" DataSourceID="LinqDataSource1"
EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="title" HeaderText="Title" />
</Columns>
</asp:GridView>
and my code:
protected void MySelecting(object sender, LinqDataSourceSelectEventArgs e)
{
TestEntitiesDataContext dc = new TestEntitiesDataContext("http://sp/sites/test");
e.Result = from item in dc.TestList
select new
{
title = item.Title,
numberField = item.NumberField.ToString()
};
}
Now the problem is when I try to view the webpart on the site I get this error:
Expression of type 'System.Int32' cannot be used for return type 'System.Object'
When I deactivate paging on grid view this error disappears.
Do you have any idea why this is happening?
I 'd be grateful of any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,以下解决方法有所帮助:
在 MySelecting 中:
如果您需要排序,您还应该处理 e.Arguments.SortExpression。
for me the following work around has helped:
in MySelecting:
And if you need sorting, you should also process e.Arguments.SortExpression.