简体   繁体   English

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

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

I have a simple page that looks up contacts using a webservice with a single method written in Linq. 我有一个简单的页面,可以使用Web服务通过Linq编写的单个方法查找联系人。 On the page, I have both a gridview and a listview with a DataPager to compare the two. 在页面上,我同时拥有一个gridview和一个带DataPager的listview来比较两者。 I can get paging working just fine with the gridview, but the Linq code has to return all of the data on each call and let the web page pick out only a page's worth... not the best solution. 我可以使分页在gridview上正常工作,但是Linq代码必须返回每个调用的所有数据,并让网页仅选择页面的价值……不是最佳解决方案。

I have been told that a ListView will solve this problem, but all the examples I have been able to find have the Linq code on the web page instead of in a separate layer (eg a webservice). 有人告诉我ListView可以解决此问题,但是我能够找到的所有示例都在网页上而不是在单独的层(例如,Web服务)中包含Linq代码。 Ideally, I should be able to tell the web service to bring back a specific page worth of data (starting record number and number of rows), but how do I get the ListView (or the DataPager) to fire an event that asks for this data? 理想情况下,我应该能够告诉Web服务带回特定页面的数据(起始记录数和行数),但是如何获取ListView(或DataPager)来触发要求此操作的事件数据?

Here is the ASPX code: 这是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>

Here's the code behind: 这是背后的代码:

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

and the web serivice: 和网络服务:

[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();
    }
}

Not 100% sure of an answer here. 这里不是100%确定答案。 Using the DataPager while binding the DataSource at runtime will add a lot of work but I think it can be done. 在运行时绑定数据源的同时使用DataPager会增加很多工作,但我认为可以做到。

Alternatively consider using either a LinqDataSource and wiring up the OnSelecting event or using an ObjectDataSource. 或者,考虑使用LinqDataSource并连接OnSelecting事件或使用ObjectDataSource。 Not sure how the DataPager works with an ODS (if at all). 不确定DataPager如何与ODS配合使用(如果有的话)。 The LinqDataSource or the ObjectDataSource will need to be associated with the ListView using the DataSourceID. LinqDataSource或ObjectDataSource将需要使用DataSourceID与ListView关联。

If you must bind using the DataSource, you can wire up the OnPagePropertiesChanging event on the ListView along with the PageSize and StartRowIndex properties on the DataPager. 如果必须使用DataSource进行绑定,则可以连接ListView上的OnPagePropertiesChanging事件以及DataPager上的PageSize和StartRowIndex属性。 You will need to search your control tree for your DataPager since it is likely contained in a template. 您将需要在控件树中搜索DataPager,因为它可能包含在模板中。

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

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