天天看點

上接擴充GridView控件(10) - 自定義分頁樣式

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((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'/>"; 

                                        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'/>"; 

                                        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'/>"; 

                                        Last.Text = PagerSettings.LastPageText; 

                                Last.CommandName = "Page"; 

                                Last.CommandArgument = "Last"; 

                                Last.Font.Underline = false; 

                                if (this.PageIndex <= 0) 

                                        First.Enabled = Prev.Enabled = false; 

                                        First.Enabled = Prev.Enabled = true; 

                                tc.Controls.Add(First); 

                                tc.Controls.Add(Prev); 

                                // 目前頁左邊顯示的數字分頁按鈕的數量 

                                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) 

                                        } 

                                        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); 

                                if (this.PageIndex >= PageCount - 1) 

                                        Next.Enabled = Last.Enabled = false; 

                                        Next.Enabled = Last.Enabled = true; 

                                tc.Controls.Add(Next); 

                                tc.Controls.Add(Last); 

                                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; 

/// 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" /> 

                                </Columns> 

                        </yyc:SmartGridView> 

                        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" 

                                TypeName="OjbData"></asp:ObjectDataSource> 

                </div> 

        </form> 

</body> 

</html>

/*測試版的實作 結束*/

OK

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/345576,如需轉載請自行聯系原作者

繼續閱讀