使用Silverlight3中的DataPager实现服务器端分页[转]
[转]blog中描述了使用DataPager实现客户端分页: public static class DataPageExtension { public static void BindSource(this DataPager dataPager, int totalCount, int pageSize) { List<int> list = new List<int>(totalCount); for (int i = 0; i < totalCount; i++) list.Add(i); PagedCollectionView pcv = new PagedCollectionView(list); pcv.PageSize = pageSize; dataPager.Source = pcv; } } WCF服务端的分页方法如下: [OperationContract] public List<MyEmployee> GetEmployeeList(EmployeeFilter filter,out int totalCount) { using (TestDBEntities db = new TestDBEntities()) { int rowsCount = 0; StringBuilder sbSql = new StringBuilder("True "); if (filter.DeptID != new Guid()) sbSql.Append(string.Format("and it.Departments.DepartmentID = Guid'{0}'", filter.DeptID)); sbSql.Append(string.Format("and it.EmpolyeeName like '%{0}%'", filter.EmpName)); var query = from emp in db.Employees.Where(sbSql.ToString()) select new MyEmployee { ID = emp.EmployeeID, Name = emp.EmpolyeeName, Sex = emp.EmployeeSex ? "男" : "女", Age = emp.EmployeeAge, Address = emp.EmployeeAddress, DeptName = emp.Departments.DepartmentName }; if (filter.PageIndex <= 0) rowsCount = query.Count(); totalCount = rowsCount; query = query.OrderBy(t => t.Name).Skip(filter.PageIndex * filter.PageSize).Take(filter.PageSize); return query.ToList(); } } 上面的代码实现了使用Entity SQl的动态查找功能和分页功能,可以看到:只有当pageindex 等于0的时候才计算总记录数,提高了方法执行的效率;方法的输入参数和输出参数都进行了实体类的封装,建议在实际项目中也这样做,特别是在使用依赖注入的时候。 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Data; using DataPagerTest.EmployeeServiceReference; namespace DataPagerTest { public partial class MainPage : UserControl { EmployeeServiceClient client = new EmployeeServiceClient(); EmployeeFilter filter = new EmployeeFilter(); public MainPage() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { dpEmployee.PageIndexChanged += new EventHandler<EventArgs>(dpEmployee_PageIndexChanged); cbDept.SelectionChanged += new SelectionChangedEventHandler(cbDept_SelectionChanged); BindCombox(); } void cbDept_SelectionChanged(object sender, SelectionChangedEventArgs e) { BindGrid(0); } void dpEmployee_PageIndexChanged(object sender, EventArgs e) { BindGrid(dpEmployee.PageIndex); } private void btnQuery_Click(object sender, RoutedEventArgs e) { BindGrid(0); } private void BindGrid(int pageIndex) { Departments dept = cbDept.SelectedItem as Departments; filter.DeptID = dept.DepartmentID; filter.EmpName = tbEmpName.Text.Trim(); filter.PageIndex = pageIndex; filter.PageSize = 9; client.GetEmployeeListCompleted += new EventHandler<GetEmployeeListCompletedEventArgs>(client_GetEmployeeListCompleted); client.GetEmployeeListAsync(filter); } void client_GetEmployeeListCompleted(object sender, GetEmployeeListCompletedEventArgs e) { dgEmployee.ItemsSource = e.Result; if (filter.PageIndex <= 0) dpEmployee.BindSource(e.totalCount, filter.PageSize); } void BindCombox() { client.GetDepartmentListCompleted += new EventHandler<GetDepartmentListCompletedEventArgs>(client_GetDepartmentListCompleted); client.GetDepartmentListAsync(); } void client_GetDepartmentListCompleted(object sender, GetDepartmentListCompletedEventArgs e) { cbDept.ItemsSource = e.Result; cbDept.DisplayMemberPath = "DepartmentName"; cbDept.SelectedIndex = 0; BindGrid(0); } } } 在PageIndex等于0的时候调用BindSource扩展方法来绑定总记录数和页大小;里面还有个关于Combobox的数据绑定方法。这样的分页方法不知大家有何评价,欢迎拍砖。 |