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

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
EMR Serverless StarRocks,5000CU*H 48000GB*H
网络型负载均衡 NLB,每月750个小时 15LCU
简介:
5、重写OnRowCreated以实现自定义分页样式
/// <summary> 
                 /// OnRowCreated 
                 /// </summary> 
                 /// <param name="e"></param> 
                 protected  override  void OnRowCreated(GridViewRowEventArgs e) 
                { 
                         if (e.Row.RowType == DataControlRowType.Pager && PagingStyle == Paging.PagingStyleCollection.Default) 
                        { 
                                LinkButton First =  new LinkButton(); 
                                LinkButton Prev =  new LinkButton(); 
                                LinkButton Next =  new LinkButton(); 
                                LinkButton Last =  new LinkButton(); 
 
                                TableCell tc =  new TableCell(); 
 
                                e.Row.Controls.Clear(); 
                                 
                                tc.Controls.Add( new LiteralControl( "  ")); 
                                 if (_recordCount.HasValue) 
                                { 
                                        tc.Controls.Add( new LiteralControl(_recordCount.ToString())); 
                                        tc.Controls.Add( new LiteralControl( "  ")); 
                                        tc.Controls.Add( new LiteralControl(PageSize.ToString())); 
                                        tc.Controls.Add( new LiteralControl( "  ")); 
                                } 
                                tc.Controls.Add( new LiteralControl((PageIndex + 1).ToString())); 
                                tc.Controls.Add( new LiteralControl( "/")); 
                                tc.Controls.Add( new LiteralControl(PageCount.ToString())); 
                                tc.Controls.Add( new LiteralControl( "    ")); 
 
                                 if (!String.IsNullOrEmpty(PagerSettings.FirstPageImageUrl)) 
                                { 
                                        First.Text =  "<img src='" + ResolveUrl(PagerSettings.FirstPageImageUrl) +  "' border='0'/>"
                                } 
                                 else 
                                { 
                                        First.Text = PagerSettings.FirstPageText; 
                                } 
                                First.CommandName =  "Page"
                                First.CommandArgument =  "First"
                                First.Font.Underline =  false
 
                                 if (!String.IsNullOrEmpty(PagerSettings.PreviousPageImageUrl)) 
                                { 
                                        Prev.Text =  "<img src='" + ResolveUrl(PagerSettings.PreviousPageImageUrl) +  "' border='0'/>"
                                } 
                                 else 
                                { 
                                        Prev.Text = PagerSettings.PreviousPageText; 
                                } 
                                Prev.CommandName =  "Page"
                                Prev.CommandArgument =  "Prev"
                                Prev.Font.Underline =  false
 
 
                                 if (!String.IsNullOrEmpty(PagerSettings.NextPageImageUrl)) 
                                { 
                                        Next.Text =  "<img src='" + ResolveUrl(PagerSettings.NextPageImageUrl) +  "' border='0'/>"
                                } 
                                 else 
                                { 
                                        Next.Text = PagerSettings.NextPageText; 
                                } 
                                Next.CommandName =  "Page"
                                Next.CommandArgument =  "Next"
                                Next.Font.Underline =  false
 
                                 if (!String.IsNullOrEmpty(PagerSettings.LastPageImageUrl)) 
                                { 
                                        Last.Text =  "<img src='" + ResolveUrl(PagerSettings.LastPageImageUrl) +  "' border='0'/>"
                                } 
                                 else 
                                { 
                                        Last.Text = PagerSettings.LastPageText; 
                                } 
                                Last.CommandName =  "Page"
                                Last.CommandArgument =  "Last"
                                Last.Font.Underline =  false
 
                                 if ( this.PageIndex <= 0) 
                                { 
                                        First.Enabled = Prev.Enabled =  false
                                } 
                                 else 
                                { 
                                        First.Enabled = Prev.Enabled =  true
                                } 
 
                                tc.Controls.Add(First); 
                                tc.Controls.Add( new LiteralControl( "  ")); 
                                tc.Controls.Add(Prev); 
                                tc.Controls.Add( new LiteralControl( "  ")); 
 
                                 // 当前页左边显示的数字分页按钮的数量 
                                 int rightCount = ( int)(PagerSettings.PageButtonCount / 2); 
                                 // 当前页右边显示的数字分页按钮的数量 
                                 int leftCount = PagerSettings.PageButtonCount % 2 == 0 ? rightCount - 1 : rightCount; 
                                 for ( int i = 0; i < PageCount; i++) 
                                { 
                                         if (PageCount > PagerSettings.PageButtonCount) 
                                        { 
                                                 if (i < PageIndex - leftCount && PageCount - 1 - i > PagerSettings.PageButtonCount - 1) 
                                                { 
                                                         continue
                                                } 
                                                 else  if (i > PageIndex + rightCount && i > PagerSettings.PageButtonCount - 1) 
                                                { 
                                                         continue
                                                } 
                                        } 
 
                                         if (i == PageIndex) 
                                        { 
                                                tc.Controls.Add( new LiteralControl( "<span style='color:red;font-weight:bold'>" + (i + 1).ToString() +  "</span>")); 
                                        } 
                                         else 
                                        { 
                                                LinkButton lb =  new LinkButton(); 
                                                lb.Text = (i + 1).ToString(); 
                                                lb.CommandName =  "Page"
                                                lb.CommandArgument = (i + 1).ToString(); 
 
                                                tc.Controls.Add(lb); 
                                        } 
 
                                        tc.Controls.Add( new LiteralControl( "  ")); 
                                } 
 
                                 if ( this.PageIndex >= PageCount - 1) 
                                { 
                                        Next.Enabled = Last.Enabled =  false
                                } 
                                 else 
                                { 
                                        Next.Enabled = Last.Enabled =  true
                                } 
                                tc.Controls.Add(Next); 
                                tc.Controls.Add( new LiteralControl( "  ")); 
                                tc.Controls.Add(Last); 
                                tc.Controls.Add( new LiteralControl( "  ")); 
 
                                tc.ColumnSpan =  this.Columns.Count; 
                                e.Row.Controls.Add(tc); 
                        } 
 
                         base.OnRowCreated(e); 
                }
 
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置PagingStyle属性为Default,同时设置GridView的原有属性PageButtonCount,FirstPageText,PreviousPageText,NextPageText,LastPageText,FirstPageImageUrl,PreviousPageImageUrl,NextPageImageUrl,LastPageImageUrl
ObjData.cs
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
 
using System.ComponentModel; 
 
/// <summary> 
/// OjbData 的摘要说明 
/// </summary> 
public  class OjbData 

         public OjbData() 
        { 
                 // 
                 // TODO: 在此处添加构造函数逻辑 
                 // 
        } 
 
        [DataObjectMethod(DataObjectMethodType.Select,  true)] 
         public DataTable Select() 
        { 
                DataTable dt =  new DataTable(); 
                dt.Columns.Add( "no"typeof( string)); 
                dt.Columns.Add( "name"typeof( string)); 
 
                 for ( int i = 0; i < 30; i++) 
                { 
                        DataRow dr = dt.NewRow(); 
                        dr[0] =  "no" + i.ToString().PadLeft(2, '0'); 
                        dr[1] =  "name" + i.ToString().PadLeft(2, '0'); 
 
                        dt.Rows.Add(dr); 
                } 
 
                 return dt; 
        } 
}
 
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
        <title>SmartGridView测试</title> 
</head> 
<body> 
        <form id="form1" runat="server"> 
                <div> 
                        <yyc:SmartGridView ID="SmartGridView1" runat="server" DataSourceID="ObjectDataSource1" 
                                AutoGenerateColumns="False" AllowPaging="true" PagingStyle="Default"> 
                                <Columns> 
                                        <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" /> 
                                        <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" ItemStyle-Width="100px" /> 
                                        <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" /> 
                                        <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" ItemStyle-Width="100px" /> 
                                        <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" /> 
                                        <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" ItemStyle-Width="100px" /> 
                                </Columns> 
                        </yyc:SmartGridView> 
                        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" 
                                TypeName="OjbData"></asp:ObjectDataSource> 
                </div> 
        </form> 
</body> 
</html>
/*测试版的实现 结束*/
 




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/345576,如需转载请自行联系原作者


相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
3月前
|
存储 开发框架 前端开发
在Winform分页控件中集成保存用户列表显示字段及宽度调整设置
在Winform分页控件中集成保存用户列表显示字段及宽度调整设置
|
6月前
【sgDragSize】自定义组件:自定义拖拽修改DIV尺寸组件,适用于窗体大小调整
【sgDragSize】自定义组件:自定义拖拽修改DIV尺寸组件,适用于窗体大小调整
|
数据库 C# 数据库管理
C#:Winfrom 实现DataGridView 自定义分页
今天给大家分享Winform实现DataGridView 自定义分页的案例,感兴趣的朋友可以一起来学习一下。 采用技术:C\#+Winform+Dapper+SQLite。
C#:Winfrom 实现DataGridView 自定义分页
|
算法
分页控件和几个相关控件的源代码
分页控件的源代码,可能会让有些人失望,因为代码很乱。乱的一个原因呢,可能是没有采用OO的思路吧,因为写控件的时候还一点都不会OO呢,只是一直在用,也就没有作大的重构。有两个分页控件, 一个是通过PostBack来分页的,一个是通过URL来分页的。
791 0