silverlight 服务器端分页实现
silverlight 分页服务端分页就是在服务里面写分页的方法,silverlight 调用的时候传参数过来就行了。先看服务代码!
我这边使用的是wcf服务!类图如下:
服务写了两个方法,PageCount这个方法是获取数据总条数,PageFilter用于分页。该方法接受三个参数,PageSize、PageIndex、Filter,当然分页的前两个参数是必不可少的,其他的参数可以根据实际情况来定义;如果你要根据用户名来过滤的话,加个string UserName就行了;废话不多说。直接看该方法的代码。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- namespace Web
- {
- // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WebPageService”。
- public class WebPageService : IWebPageService
- {
- public List<PageEntity> PageFilter(int? PageSize,int PageIndex,string filter)
- {
- List<PageEntity> sources = PageEntity.CreatePageSources();
- return sources.Skip((PageSize * (PageIndex - 1)).Value).Take(PageSize.Value).ToList();
- }
- public int PageCount(string filter)
- {
- return 6;
- }
- }
- public class PageEntity
- {
- public static List<PageEntity> CreatePageSources()
- {
- List<PageEntity> entitys = new List<PageEntity>();
- entitys.Add(new PageEntity()
- {
- UserName="aa",
- UserSex="男"
- });
- entitys.Add(new PageEntity()
- {
- UserName = "aaa",
- UserSex = "女"
- });
- entitys.Add(new PageEntity()
- {
- UserName = "aaa1",
- UserSex = "女"
- });
- entitys.Add(new PageEntity()
- {
- UserName = "aaa2",
- UserSex = "男"
- });
- entitys.Add(new PageEntity()
- {
- UserName = "aaa3",
- UserSex = "女"
- });
- entitys.Add(new PageEntity()
- {
- UserName = "aaa4",
- UserSex = "男"
- });
- return entitys;
- }
- public string UserName { get; set; }
- public string UserSex { get; set; }
- public int DataCount { get; set; }
- }
- }
PageFilter方法很简单,就通过LInq分下页,你也可以通过其他方式分页,调用存储过程等!这个服务写好了。现在要做的就是silverlight 程序里面引用这个服务;
- <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="Silverlight.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- d:DesignHeight="300" d:DesignWidth="400">
- <Grid x:Name="LayoutRoot" Background="White">
- <StackPanel>
- <sdk:DataGrid x:Name="dataGrid" Height="200"/>
- <sdk:DataPager x:Name="pager1" PageSize="2" PageIndexChanged="pager1_PageIndexChanged"></sdk:DataPager>
- </StackPanel>
- </Grid>
- </UserControl>
这个页面就放了一个DataGrid和DataPager;简单说下这个分页控件,点击分页的时候他会触发PageIndexChanged事件,还有在设置分页控件的Source属性的时候他也会触发PageIndexChanged事件!
我的做法就是当服务器返回的数据总数和分页控件的数据总数不一致的时候在从新设置分页控件的Source;贴代码!
- 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 Silverlight.Test.PageService;
- using System.Windows.Data;
- namespace Silverlight
- {
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- Filter();
- }
- private void pager1_PageIndexChanged(object sender,EventArgs e)
- {
- Filter();
- }
- private void Filter()
- {
- WebPageServiceClient client = new WebPageServiceClient();
- client.PageCountCompleted += new EventHandler<PageCountCompletedEventArgs>(client_PageCountCompleted);
- client.PageCountAsync(string.Empty);
- }
- void client_PageCountCompleted(object sender,PageCountCompletedEventArgs e)
- {
- if (e.Result < 1)
- return;
- SetPageSource(e.Result);
- var pageIndex = pager1.PageIndex + 1;
- WebPageServiceClient client = sender as WebPageServiceClient;
- client.PageFilterCompleted += new EventHandler<PageFilterCompletedEventArgs>(client_PageFilterCompleted);
- client.PageFilterasync(pager1.PageSize,pageIndex,string.Empty);
- }
- void client_PageFilterCompleted(object sender,PageFilterCompletedEventArgs e)
- {
- dataGrid.ItemsSource = e.Result;
- }
- private void SetPageSource(int resultCount)
- {
- if (pager1.source != null && (pager1.source as PagedCollectionView).TotalItemCount == resultCount)
- return;
- List<int> source = new List<int>();
- for (int i = 0; i < resultCount; i++)
- {
- source.Add(i);
- }
- PagedCollectionView pagedCollection = new PagedCollectionView(source);
- pager1.source = pagedCollection;
- }
- }
- }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。