0

我有一个简单的页面,它使用一个用 Linq 编写的单一方法使用 web 服务查找联系人。在页面上,我有一个 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();
    }
}
4

1 回答 1

0

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

或者考虑使用 LinqDataSource 并连接 OnSelecting 事件或使用 ObjectDataSource。不确定 DataPager 如何与 ODS 一起工作(如果有的话)。LinqDataSource 或 ObjectDataSource 需要使用 DataSourceID 与 ListView 相关联。

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

于 2010-12-20T04:19:00.523 回答