我有一个简单的页面,它使用一个用 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();
}
}