簡體   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