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

5、重写OnRowCreated以实现自定义分页样式
/// <summary> 
InBlock.gif                 /// OnRowCreated 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="e"></param> 
InBlock.gif                 protected  override  void OnRowCreated(GridViewRowEventArgs e) 
InBlock.gif                { 
InBlock.gif                         if (e.Row.RowType == DataControlRowType.Pager && PagingStyle == Paging.PagingStyleCollection.Default) 
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                                e.Row.Controls.Clear(); 
InBlock.gif                                 
InBlock.gif                                tc.Controls.Add( new LiteralControl( "  ")); 
InBlock.gif                                 if (_recordCount.HasValue) 
InBlock.gif                                { 
InBlock.gif                                        tc.Controls.Add( new LiteralControl(_recordCount.ToString())); 
InBlock.gif                                        tc.Controls.Add( new LiteralControl( "  ")); 
InBlock.gif                                        tc.Controls.Add( new LiteralControl(PageSize.ToString())); 
InBlock.gif                                        tc.Controls.Add( new LiteralControl( "  ")); 
InBlock.gif                                } 
InBlock.gif                                tc.Controls.Add( new LiteralControl((PageIndex + 1).ToString())); 
InBlock.gif                                tc.Controls.Add( new LiteralControl( "/")); 
InBlock.gif                                tc.Controls.Add( new LiteralControl(PageCount.ToString())); 
InBlock.gif                                tc.Controls.Add( new LiteralControl( "    ")); 
InBlock.gif 
InBlock.gif                                 if (!String.IsNullOrEmpty(PagerSettings.FirstPageImageUrl)) 
InBlock.gif                                { 
InBlock.gif                                        First.Text =  "<img src='" + ResolveUrl(PagerSettings.FirstPageImageUrl) +  "' border='0'/>"
InBlock.gif                                } 
InBlock.gif                                 else 
InBlock.gif                                { 
InBlock.gif                                        First.Text = PagerSettings.FirstPageText; 
InBlock.gif                                } 
InBlock.gif                                First.CommandName =  "Page"
InBlock.gif                                First.CommandArgument =  "First"
InBlock.gif                                First.Font.Underline =  false
InBlock.gif 
InBlock.gif                                 if (!String.IsNullOrEmpty(PagerSettings.PreviousPageImageUrl)) 
InBlock.gif                                { 
InBlock.gif                                        Prev.Text =  "<img src='" + ResolveUrl(PagerSettings.PreviousPageImageUrl) +  "' border='0'/>"
InBlock.gif                                } 
InBlock.gif                                 else 
InBlock.gif                                { 
InBlock.gif                                        Prev.Text = PagerSettings.PreviousPageText; 
InBlock.gif                                } 
InBlock.gif                                Prev.CommandName =  "Page"
InBlock.gif                                Prev.CommandArgument =  "Prev"
InBlock.gif                                Prev.Font.Underline =  false
InBlock.gif 
InBlock.gif 
InBlock.gif                                 if (!String.IsNullOrEmpty(PagerSettings.NextPageImageUrl)) 
InBlock.gif                                { 
InBlock.gif                                        Next.Text =  "<img src='" + ResolveUrl(PagerSettings.NextPageImageUrl) +  "' border='0'/>"
InBlock.gif                                } 
InBlock.gif                                 else 
InBlock.gif                                { 
InBlock.gif                                        Next.Text = PagerSettings.NextPageText; 
InBlock.gif                                } 
InBlock.gif                                Next.CommandName =  "Page"
InBlock.gif                                Next.CommandArgument =  "Next"
InBlock.gif                                Next.Font.Underline =  false
InBlock.gif 
InBlock.gif                                 if (!String.IsNullOrEmpty(PagerSettings.LastPageImageUrl)) 
InBlock.gif                                { 
InBlock.gif                                        Last.Text =  "<img src='" + ResolveUrl(PagerSettings.LastPageImageUrl) +  "' border='0'/>"
InBlock.gif                                } 
InBlock.gif                                 else 
InBlock.gif                                { 
InBlock.gif                                        Last.Text = PagerSettings.LastPageText; 
InBlock.gif                                } 
InBlock.gif                                Last.CommandName =  "Page"
InBlock.gif                                Last.CommandArgument =  "Last"
InBlock.gif                                Last.Font.Underline =  false
InBlock.gif 
InBlock.gif                                 if ( this.PageIndex <= 0) 
InBlock.gif                                { 
InBlock.gif                                        First.Enabled = Prev.Enabled =  false
InBlock.gif                                } 
InBlock.gif                                 else 
InBlock.gif                                { 
InBlock.gif                                        First.Enabled = Prev.Enabled =  true
InBlock.gif                                } 
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 
InBlock.gif                                 // 当前页左边显示的数字分页按钮的数量 
InBlock.gif                                 int rightCount = ( int)(PagerSettings.PageButtonCount / 2); 
InBlock.gif                                 // 当前页右边显示的数字分页按钮的数量 
InBlock.gif                                 int leftCount = PagerSettings.PageButtonCount % 2 == 0 ? rightCount - 1 : rightCount; 
InBlock.gif                                 for ( int i = 0; i < PageCount; i++) 
InBlock.gif                                { 
InBlock.gif                                         if (PageCount > PagerSettings.PageButtonCount) 
InBlock.gif                                        { 
InBlock.gif                                                 if (i < PageIndex - leftCount && PageCount - 1 - i > PagerSettings.PageButtonCount - 1) 
InBlock.gif                                                { 
InBlock.gif                                                         continue
InBlock.gif                                                } 
InBlock.gif                                                 else  if (i > PageIndex + rightCount && i > PagerSettings.PageButtonCount - 1) 
InBlock.gif                                                { 
InBlock.gif                                                         continue
InBlock.gif                                                } 
InBlock.gif                                        } 
InBlock.gif 
InBlock.gif                                         if (i == PageIndex) 
InBlock.gif                                        { 
InBlock.gif                                                tc.Controls.Add( new LiteralControl( "<span style='color:red;font-weight:bold'>" + (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 
InBlock.gif                                 if ( this.PageIndex >= PageCount - 1) 
InBlock.gif                                { 
InBlock.gif                                        Next.Enabled = Last.Enabled =  false
InBlock.gif                                } 
InBlock.gif                                 else 
InBlock.gif                                { 
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 
InBlock.gif                                tc.ColumnSpan =  this.Columns.Count; 
InBlock.gif                                e.Row.Controls.Add(tc); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                         base.OnRowCreated(e); 
InBlock.gif                }
 
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置PagingStyle属性为Default,同时设置GridView的原有属性PageButtonCount,FirstPageText,PreviousPageText,NextPageText,LastPageText,FirstPageImageUrl,PreviousPageImageUrl,NextPageImageUrl,LastPageImageUrl
ObjData.cs
InBlock.gif using System; 
InBlock.gif using System.Data; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gif using System.ComponentModel; 
InBlock.gif 
/// <summary> 
/// OjbData 的摘要说明 
/// </summary> 
InBlock.gif public  class OjbData 
InBlock.gif
InBlock.gif         public OjbData() 
InBlock.gif        { 
InBlock.gif                 // 
InBlock.gif                 // TODO: 在此处添加构造函数逻辑 
InBlock.gif                 // 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        [DataObjectMethod(DataObjectMethodType.Select,  true)] 
InBlock.gif         public DataTable Select() 
InBlock.gif        { 
InBlock.gif                DataTable dt =  new DataTable(); 
InBlock.gif                dt.Columns.Add( "no"typeof( string)); 
InBlock.gif                dt.Columns.Add( "name"typeof( string)); 
InBlock.gif 
InBlock.gif                 for ( int i = 0; i < 30; i++) 
InBlock.gif                { 
InBlock.gif                        DataRow dr = dt.NewRow(); 
InBlock.gif                        dr[0] =  "no" + i.ToString().PadLeft(2, '0'); 
InBlock.gif                        dr[1] =  "name" + i.ToString().PadLeft(2, '0'); 
InBlock.gif 
InBlock.gif                        dt.Rows.Add(dr); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 return dt; 
InBlock.gif        } 
InBlock.gif}
 
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>
InBlock.gif /*测试版的实现 结束*/
 




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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值