使用Silverlight3中的DataPager实现服务器端分页

使用Silverlight3中的DataPager实现服务器端分页

代振军同学的blog中描述了使用DataPager实现客户端分页:
http://www.cnblogs.com/daizhj/archive/2009/08/07/1529331.html
一般说来,在项目中一般很少使用这种客户端的分页方式,除非数据量很少(干脆不分页算了)。
把大量的数据一次性传输到客户端可不是个明智的做法,我们一般都是从客户端传入查找条件参数(包括过滤条件和分页条件参数),然后服务端从数据库中找出符合查找条件的的记录列表传输给客户端,客户端绑定到DataGrid控件上。
这里使用“开启了Silverlight的WCF服务”来和客户端(silverlight)程序进行通信,数据访问采用Ado.net Entity Framework,解决方案结构如图:

建立了一个测试用的数据库:包含两张表:

该实例除了演示了使用DataPager的服务器端分页外还实现了使用Entity SQL的动态查找功能:

分页控件很多人都写过,但不像DataPager那样要传入个PagedCollectionView才行,一般传入总记录数和分页大小就可以了,所以,我们给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的时候才计算总记录数,提高了方法执行的效率;方法的输入参数和输出参数都进行了实体类的封装,建议在实际项目中也这样做,特别是在使用依赖注入的时候。
在客户端(Silverlight项目)中引用好服务后,页面的cs代码如下:


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的数据绑定方法。这样的分页方法不知大家有何评价,欢迎拍砖。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值