繁体   English   中英

分页无法使用Linq与ListView和Webservice一起使用

[英]Paging not working with ListView and Webservice using Linq

我有一个简单的页面,可以使用Web服务通过Linq编写的单个方法查找联系人。 在页面上,我同时拥有一个gridview和一个带DataPager的listview来比较两者。 我可以使分页在gridview上正常工作,但是Linq代码必须返回每个调用的所有数据,并让网页仅选择页面的价值……不是最佳解决方案。

有人告诉我ListView可以解决此问题,但是我能够找到的所有示例都在网页上而不是在单独的层(例如,Web服务)中包含Linq代码。 理想情况下,我应该能够告诉Web服务带回特定页面的数据(起始记录数和行数),但是如何获取ListView(或DataPager)来触发要求此操作的事件数据?

这是ASPX代码:

    <asp:ListView ID="listPersons" runat="server">
    <LayoutTemplate>
        <table>
            <thead>
                <tr>
                    <th>
                        Site ID
                    </th>
                    <th>
                        PersonID
                    </th>
                    <th>
                        Person Name
                    </th>
            </thead>
            <tbody>
                <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
            </tbody>
        </table>
        <asp:DataPager ID="Pager1" runat="server" PagedControlID="listPersons" PageSize="5" >
            <Fields>
                <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowPreviousPageButton="true"
                    ShowNextPageButton="false" ShowLastPageButton="false" />
                <asp:NumericPagerField />
                <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="false"
                    ShowNextPageButton="true" ShowLastPageButton="true" />
            </Fields>
        </asp:DataPager>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%# Eval("SiteID") %>
            </td>
            <td>
                <%# Eval("PersonID") %>
            </td>
            <td>
                <%# Eval("PersonName") %>
            </td>
        </tr>
    </ItemTemplate>
    <EmptyDataTemplate>
        No data found...
    </EmptyDataTemplate>
</asp:ListView>

这是背后的代码:

private void DoList(string Match)
{
    ContactsService cs = new ContactsService();
    listPersons.DataSource = cs.Find(Match, 100 );
    listPersons.DataBind();
}

和网络服务:

[WebMethod]
public List<Person>Find(string Match, int Count)
{
    if (Count < 5) Count = 5;
    using (DataLayer.ContactsDataContext context = new ContactsDataContext())
    {
        var Persons =
            from p in context.Persons
            where p.PersonName.Contains(Match)
            orderby p.LastName, p.FirstName
            select new Person()
            {
                SiteID = p.SiteID,
                PersonID = p.PersonID,
                PersonName = p.PersonName,
            };
        return Persons.Take(Count).ToList();
    }
}

这里不是100%确定答案。 在运行时绑定数据源的同时使用DataPager会增加很多工作,但我认为可以做到。

或者,考虑使用LinqDataSource并连接OnSelecting事件或使用ObjectDataSource。 不确定DataPager如何与ODS配合使用(如果有的话)。 LinqDataSource或ObjectDataSource将需要使用DataSourceID与ListView关联。

如果必须使用DataSource进行绑定,则可以连接ListView上的OnPagePropertiesChanging事件以及DataPager上的PageSize和StartRowIndex属性。 您将需要在控件树中搜索DataPager,因为它可能包含在模板中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM