GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[索引页]
[×××]


扩展GridView控件(10) - 自定义分页样式


作者: webabcd
InBlock.gif /*正式版的实现 开始*/
 
介绍
扩展GridView控件:
自定义分页样式。显示总记录数、每页记录数、当前页数、总页数、首页、上一页、下一页、末页和分页按钮

使用方法(设置CustomPagerSettings复合属性):
PagingMode - 自定义分页的显示模式
TextFormat - 自定义分页的文本显示样式(四个占位符:{0}-每页显示记录数;{1}-总记录数;{2}-当前页数;{3}-总页数)


关键代码
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Text;
InBlock.gif
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.Web.UI;
InBlock.gif using System.ComponentModel;
InBlock.gif using System.Collections;
InBlock.gif using System.Data;
InBlock.gif using System.Web.UI.HtmlControls;
InBlock.gif
InBlock.gif namespace YYControls.SmartGridViewFunction
InBlock.gif{
InBlock.gif         /// <summary>
InBlock.gif         /// 扩展功能:自定义分页样式
InBlock.gif         /// </summary>
InBlock.gif         public class CustomPagerSettingsFunction : ExtendFunction
InBlock.gif        {
InBlock.gif                 /// <summary>
InBlock.gif                 /// 构造函数
InBlock.gif                 /// </summary>
InBlock.gif                 public CustomPagerSettingsFunction()
InBlock.gif                        : base()
InBlock.gif                {
InBlock.gif
InBlock.gif                }
InBlock.gif
InBlock.gif                 /// <summary>
InBlock.gif                 /// 构造函数
InBlock.gif                 /// </summary>
InBlock.gif                 /// <param name="sgv">SmartGridView对象</param>
InBlock.gif                 public CustomPagerSettingsFunction(SmartGridView sgv)
InBlock.gif                        : base(sgv)
InBlock.gif                {
InBlock.gif        
InBlock.gif                }
InBlock.gif
InBlock.gif                 /// <summary>
InBlock.gif                 /// 扩展功能的实现
InBlock.gif                 /// </summary>
InBlock.gif                 protected override void Execute()
InBlock.gif                {
InBlock.gif                         this._sgv.InitPager += new SmartGridView.InitPagerHandler(_sgv_InitPager);    
InBlock.gif                }
InBlock.gif
InBlock.gif                 /// <summary>
InBlock.gif                 /// SmartGridView的InitPager事件
InBlock.gif                 /// </summary>
InBlock.gif                 /// <param name="sender"></param>
InBlock.gif                 /// <param name="row">一个 System.Web.UI.WebControls.GridViewRow,表示要初始化的页导航行</param>
InBlock.gif                 /// <param name="columnSpan">页导航行应跨越的列数</param>
InBlock.gif                 /// <param name="pagedDataSource">一个 System.Web.UI.WebControls.PagedDataSource,表示数据源</param>
InBlock.gif                 void _sgv_InitPager( object sender, GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
InBlock.gif                {
InBlock.gif                         int recordCount = pagedDataSource.DataSourceCount;
InBlock.gif
InBlock.gif                        LinkButton First = new LinkButton();
InBlock.gif                        LinkButton Prev = new LinkButton();
InBlock.gif                        LinkButton Next = new LinkButton();
InBlock.gif                        LinkButton Last = new LinkButton();
InBlock.gif
InBlock.gif                        TableCell tc = new TableCell();
InBlock.gif
InBlock.gif                        row.Controls.Clear();
InBlock.gif
InBlock.gif                        tc.Controls.Add( new LiteralControl( " "));
InBlock.gif
InBlock.gif显示总记录数 每页记录数 当前页数/总页数 #region 显示总记录数 每页记录数 当前页数/总页数
InBlock.gif                         string textFormat = String.Format( this._sgv.CustomPagerSettings.TextFormat,
InBlock.gif                                pagedDataSource.PageSize,
InBlock.gif                                pagedDataSource.DataSourceCount,
InBlock.gif                                pagedDataSource.CurrentPageIndex + 1,
InBlock.gif                                pagedDataSource.PageCount);
InBlock.gif                        tc.Controls.Add( new LiteralControl(textFormat));
InBlock.gif                        #endregion
InBlock.gif
InBlock.gif设置“首页 上一页 下一页 末页”按钮 #region 设置“首页 上一页 下一页 末页”按钮
InBlock.gif                         if (!String.IsNullOrEmpty( this._sgv.PagerSettings.FirstPageImageUrl))
InBlock.gif                                First.Text = "<img src='" + this._sgv.ResolveUrl( this._sgv.PagerSettings.FirstPageImageUrl) + "' border='0'/>";
InBlock.gif                         else
InBlock.gif                                First.Text = this._sgv.PagerSettings.FirstPageText;
InBlock.gif
InBlock.gif                        First.CommandName = "Page";
InBlock.gif                        First.CommandArgument = "First";
InBlock.gif
InBlock.gif                         if (!String.IsNullOrEmpty( this._sgv.PagerSettings.PreviousPageImageUrl))
InBlock.gif                                Prev.Text = "<img src='" + this._sgv.ResolveUrl( this._sgv.PagerSettings.PreviousPageImageUrl) + "' border='0'/>";
InBlock.gif                         else
InBlock.gif                                Prev.Text = this._sgv.PagerSettings.PreviousPageText;
InBlock.gif
InBlock.gif                        Prev.CommandName = "Page";
InBlock.gif                        Prev.CommandArgument = "Prev";
InBlock.gif
InBlock.gif
InBlock.gif                         if (!String.IsNullOrEmpty( this._sgv.PagerSettings.NextPageImageUrl))
InBlock.gif                                Next.Text = "<img src='" + this._sgv.ResolveUrl( this._sgv.PagerSettings.NextPageImageUrl) + "' border='0'/>";
InBlock.gif                         else
InBlock.gif                                Next.Text = this._sgv.PagerSettings.NextPageText;
InBlock.gif
InBlock.gif                        Next.CommandName = "Page";
InBlock.gif                        Next.CommandArgument = "Next";
InBlock.gif
InBlock.gif                         if (!String.IsNullOrEmpty( this._sgv.PagerSettings.LastPageImageUrl))
InBlock.gif                                Last.Text = "<img src='" + this._sgv.ResolveUrl( this._sgv.PagerSettings.LastPageImageUrl) + "' border='0'/>";
InBlock.gif                         else
InBlock.gif                                Last.Text = this._sgv.PagerSettings.LastPageText;
InBlock.gif
InBlock.gif                        Last.CommandName = "Page";
InBlock.gif                        Last.CommandArgument = "Last";
InBlock.gif                        #endregion
InBlock.gif
InBlock.gif添加首页,上一页按钮 #region 添加首页,上一页按钮
InBlock.gif                         if ( this._sgv.PageIndex <= 0)
InBlock.gif                                First.Enabled = Prev.Enabled = false;
InBlock.gif                         else
InBlock.gif                                First.Enabled = Prev.Enabled = true;
InBlock.gif
InBlock.gif                        tc.Controls.Add(First);
InBlock.gif                        tc.Controls.Add( new LiteralControl( " "));
InBlock.gif                        tc.Controls.Add(Prev);
InBlock.gif                        tc.Controls.Add( new LiteralControl( " "));
InBlock.gif                        #endregion
InBlock.gif
InBlock.gif显示数字分页按钮 #region 显示数字分页按钮
InBlock.gif                         // 当前页左边显示的数字分页按钮的数量
InBlock.gif                         int rightCount = ( int)( this._sgv.PagerSettings.PageButtonCount / 2);
InBlock.gif                         // 当前页右边显示的数字分页按钮的数量
InBlock.gif                         int leftCount = this._sgv.PagerSettings.PageButtonCount % 2 == 0 ? rightCount - 1 : rightCount;
InBlock.gif                         for ( int i = 0; i < this._sgv.PageCount; i++)
InBlock.gif                        {
InBlock.gif                                 if ( this._sgv.PageCount > this._sgv.PagerSettings.PageButtonCount)
InBlock.gif                                {
InBlock.gif                                         if (i < this._sgv.PageIndex - leftCount && this._sgv.PageCount - 1 - i > this._sgv.PagerSettings.PageButtonCount - 1)
InBlock.gif                                        {
InBlock.gif                                                 continue;
InBlock.gif                                        }
InBlock.gif                                         else if (i > this._sgv.PageIndex + rightCount && i > this._sgv.PagerSettings.PageButtonCount - 1)
InBlock.gif                                        {
InBlock.gif                                                 continue;
InBlock.gif                                        }
InBlock.gif                                }
InBlock.gif
InBlock.gif                                 if (i == this._sgv.PageIndex)
InBlock.gif                                {
InBlock.gif                                        tc.Controls.Add( new LiteralControl( "<span>" + (i + 1).ToString() + "</span>"));
InBlock.gif                                }
InBlock.gif                                 else
InBlock.gif                                {
InBlock.gif                                        LinkButton lb = new LinkButton();
InBlock.gif                                        lb.Text = (i + 1).ToString();
InBlock.gif                                        lb.CommandName = "Page";
InBlock.gif                                        lb.CommandArgument = (i + 1).ToString();
InBlock.gif
InBlock.gif                                        tc.Controls.Add(lb);
InBlock.gif                                }
InBlock.gif
InBlock.gif                                tc.Controls.Add( new LiteralControl( " "));
InBlock.gif                        }
InBlock.gif                        #endregion
InBlock.gif
InBlock.gif添加下一页,末页按钮 #region 添加下一页,末页按钮
InBlock.gif                         if ( this._sgv.PageIndex >= this._sgv.PageCount - 1)
InBlock.gif                                Next.Enabled = Last.Enabled = false;
InBlock.gif                         else
InBlock.gif                                Next.Enabled = Last.Enabled = true;
InBlock.gif
InBlock.gif                        tc.Controls.Add(Next);
InBlock.gif                        tc.Controls.Add( new LiteralControl( " "));
InBlock.gif                        tc.Controls.Add(Last);
InBlock.gif                        tc.Controls.Add( new LiteralControl( " "));
InBlock.gif                        #endregion
InBlock.gif
InBlock.gif                        tc.Controls.Add( new LiteralControl( " "));
InBlock.gif
InBlock.gif                        tc.ColumnSpan = this._sgv.Columns.Count;
InBlock.gif
InBlock.gif                        row.Controls.Add(tc);                
InBlock.gif                }
InBlock.gif        }
InBlock.gif}
 
InBlock.gif /*正式版的实现 结束*/
 
InBlock.gif /*测试版的实现 开始*/
 
介绍
用着GridView自带的分页样式总觉得不太习惯,我们可以在PagerTemplate中来写一些自定义的样式,但是也挺麻烦的,其实我们可以扩展一下GridView,给它再增加一种分页样式


控件开发
1、新建一个继承自GridView的类。
/// <summary>
/// 继承自GridView
/// </summary>
InBlock.gif[ToolboxData( @"<{0}:SmartGridView runat='server'></{0}:SmartGridView>")]
InBlock.gif public class SmartGridView : GridView
InBlock.gif{
InBlock.gif}
 
2、新建一个Paging类,定义一个分页样式的枚举
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Text;
InBlock.gif
InBlock.gif using System.ComponentModel;
InBlock.gif
InBlock.gif namespace YYControls.SmartGridView
InBlock.gif{
InBlock.gif         /// <summary>
InBlock.gif         /// 自定义分页相关
InBlock.gif         /// </summary>
InBlock.gif         public class Paging
InBlock.gif        {
InBlock.gif                 /// <summary>
InBlock.gif                 /// 自定义分页样式
InBlock.gif                 /// </summary>
InBlock.gif                 public enum PagingStyleCollection
InBlock.gif                {
InBlock.gif                         /// <summary>
InBlock.gif                         /// 不用自定义分页样式
InBlock.gif                         /// </summary>
InBlock.gif                        None,
InBlock.gif                         /// <summary>
InBlock.gif                         /// 默认自定义分页样式
InBlock.gif                         /// </summary>
InBlock.gif                        Default
InBlock.gif                }
InBlock.gif        }
InBlock.gif}
 
3、在继承自GridView的类中加一个上面定义的枚举属性
InBlock.gif private Paging.PagingStyleCollection _pagingStyle;
InBlock.gif                 /// <summary>
InBlock.gif                 /// 自定义分页样式
InBlock.gif                 /// </summary>
InBlock.gif                [Description( "自定义分页样式"), DefaultValue( ""), Category("扩展")]
InBlock.gif                 public Paging.PagingStyleCollection PagingStyle
InBlock.gif                {
InBlock.gif                        get { return _pagingStyle; }
InBlock.gif                        set { _pagingStyle = value; }
InBlock.gif                }
4、如果GridView使用的是数据源控件的话,计算总记录数
/// <summary>
InBlock.gif                 /// OnLoad
InBlock.gif                 /// </summary>
InBlock.gif                 /// <param name="e"></param>
InBlock.gif                 protected override void OnLoad(EventArgs e)
InBlock.gif                {
InBlock.gif                         // 查找ObjectDataSource
InBlock.gif                        ObjectDataSource ods = Parent.FindControl( this.DataSourceID) as ObjectDataSource;
InBlock.gif                         if (ods != null)
InBlock.gif                        {
InBlock.gif                                ods.Selected += new ObjectDataSourceStatusEventHandler(ods_Selected);
InBlock.gif                        }
InBlock.gif
InBlock.gif                         base.OnLoad(e);
InBlock.gif                }
InBlock.gif
InBlock.gif                 private int? _recordCount = null;
InBlock.gif                 /// <summary>
InBlock.gif                 /// 计算总记录数
InBlock.gif                 /// </summary>
InBlock.gif                 /// <param name="sender"></param>
InBlock.gif                 /// <param name="e"></param>
InBlock.gif                 private void ods_Selected( object sender, ObjectDataSourceStatusEventArgs e)
InBlock.gif                {
InBlock.gif                         if (e.ReturnValue is IListSource)
InBlock.gif                        {
InBlock.gif                                _recordCount = ((IListSource)e.ReturnValue).GetList().Count;
InBlock.gif                        }
InBlock.gif                }