开发者

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. On the page, I have both a gridview and a listview with a DataPager to compare the two. 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.

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 (e.g. a webservice). 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?

Here is the ASPX code:

    <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(开发者_Go百科"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. Using the DataPager while binding the DataSource at runtime will add a lot of work but I think it can be done.

Alternatively consider using either a LinqDataSource and wiring up the OnSelecting event or using an ObjectDataSource. Not sure how the DataPager works with an ODS (if at all). The LinqDataSource or the ObjectDataSource will need to be associated with the ListView using the DataSourceID.

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. You will need to search your control tree for your DataPager since it is likely contained in a template.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜