代振军同学的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增加一个扩展方法来绑定总记录数和分页大小:

Code
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服务端的分页方法如下:

Code
 [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代码如下:

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

2009-08-18 更新:修改了数据库的Departments表结构,实现了在Combobox里显示TreeView的效果(如效果图)。

使用Silverlight3中的DataPager实现服务器端分页相关推荐

  1. 使用Telerik的DataPager进行服务器端分页

    说是服务器端,因我是WPF程序,也就是客户端了.主要是DataPager默认是一次获取全部数据,然后只显示一部分数据,如果数据量比较大,性能会有一定的影响. 我的主要做法是:根据查询得到的数据总量,生 ...

  2. datatables.js 简单使用--多选框和服务器端分页

    说明:datatables是一款jQuery表格插件.感觉EasyUI的datagrid更易用 内容:多选框和服务器端分页 缘由:写这篇博客的原因是datatables的文档写的不怎么样,找东西很麻烦 ...

  3. java服务器端分页_使用数据表的服务器端分页

    服务器每页返回15条记录,总记录超过2000条 . 我想显示前15条记录,然后每次单击"下一步"按钮,显示剩余的所有记录(每页15条记录) . 为此,我们做服务器端分页或客户端?? ...

  4. 使用ASP.NET Core和Angular 8的服务器端分页

    目录 介绍 如何工作? 先决条件 使用代码 后端 步骤1 步骤2 步骤3 Web API 步骤1 步骤2 步骤3 步骤4 步骤5 步骤6 步骤7 步骤8 步骤9 完整的分页控制器代码 前端 步骤1 步 ...

  5. jquery datatables-1.8.2服务器端分页不支持IE6,IE7 UBG修改。

    jquery datatable服务器端分页不支持IE6,IE7: 原因是在IE6,IE7,jquery datatable通过ajax 无法发送get类型请求: 但可以发送post请求,只要将插件中 ...

  6. datatables服务器端分页

    Datatables入坑指南01: 今天尝试将datatables从客户端分页转化为服务器端分页,根据官网要求我返回了相应的相应参数,具体如下: 但是客户端总是显示不了相应的json数据,最后终于发现 ...

  7. ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页

    关键字:DataGrid.存储过程.分页 出自: http://blog.csdn.net/yzx110/archive/2004/08/18/78525.aspx 摘要:在最进的一个项目中因为一个管 ...

  8. 【转】在SQL Server中通过SQL语句实现分页查询

    在SQL Server中通过SQL语句实现分页查询 2008年01月06日 星期日 12:28 建立表: CREATE TABLE [TestTable] ( [ID] [int] IDENTITY ...

  9. RDIFramework.NET 中多表关联查询分页实例

    RDIFramework.NET 中多表关联查询分页实例 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,给用户和开发者最佳的.Net框架部署方案.该框架以SOA范式作为 ...

最新文章

  1. 为什么excel图片会变成代码_莲藕为什么会变色?焯水就发黑,炖汤就变粉色,甚至会变成暗紫色...
  2. 简陋的会计凭证金额输入控件
  3. tensorflow和python先学哪个-前辈说先学会了这些Python知识点,再谈学习人工智能!...
  4. python多线程模块_python 多线程模块参考
  5. 具有ESB,API管理和Now ..服务网格的应用程序网络功能。
  6. 数据库面试中常用的10个问题
  7. python爬取商城数据_Python爬取新版CRMEB小程序商城后台订单数据,保存为excel
  8. Oracle全文索引之二 创建
  9. 量化中需留意的坑之二
  10. Sublime下MarkDown插件实现编辑和实时预览并转换成HTML格式
  11. mvc2 在 .net 4.0 下的ValidateInput(false) 无效
  12. No controller found
  13. csdn积分怎么获得
  14. HTML网页设计结课作业——19张精美网页!
  15. ios分屏_【iOS越狱】越狱源+插件整理更新
  16. Arcgis 地理配准步骤(底图校正)
  17. P1338 末日的传说(C++_数论_递推)
  18. Linux kernel + busybox自制Linux系统
  19. html文件打开自动跳转至空白
  20. 日了。这个竟然还不让发

热门文章

  1. 安装lynis_lynis安装和扫描Linux的安全漏洞
  2. linux 控制台输入命令无效_在控制台输入什么命令都提示commandnotfound原因是什么?suselinux...
  3. linux 查看java最大内存配置,Linux和Windows下的内存设置
  4. Zookeeper分布式一致性原理(二):一致性协议
  5. python进度条 pyqt_Python高级进阶#015 pyqt5进度条QProgressBar结合使用qbasictimer
  6. NRF52810能不能替代NRF52832
  7. socket层内容详解二
  8. Ceph 存储集群7-故障排除
  9. 建库、建表、建约束、插入测试数据
  10. /etc/shadow 密码加密方法