1. 程式人生 > >一個比較好的Asp.net的分頁程式碼,檢索速度很快!

一個比較好的Asp.net的分頁程式碼,檢索速度很快!

一個非常快的基於SQL Server和Asp.net的的分頁程式,使用的時候,請根據自己的需要修改必要的程式碼後再使用,這裡我就不寫怎麼改了,相信用.net的人,應該是沒問題的,下面的程式碼都應該看得懂。*^_^*

首先,.aspx部分

<asp:GridView ID="gvVisitor" runat="server"> 
     <Columns> 
         <asp:TemplateField HeaderText="序號"> 
         <ItemStyle Width="40px"/> 
         <ItemTemplate
>
<%# DataBinder.Eval(Container.DataItem, "ID")%></ItemTemplate> </asp:TemplateField> <%-- 類似的TemplateField,省略--%>...... </Columns> </asp:GridView> <%-- 以下為分頁所使用的連結,含“記錄數”“當前頁”“頁數”“首頁”“上一頁”“下一頁”“尾頁”“頁碼跳轉” --%> <div id="StatisticsPages"
>
<asp:Label ID="lbTotalRecord" runat="server" SkinID="Page"/> 條記錄 第 <asp:Label ID="lbCurrentPage" runat="server" SkinID="Page"/> 頁 / 共 <asp:Label ID="lbTotalPage" runat="server" SkinID="Page"/><asp:LinkButton ID="lbFirstPage" runat="server" CommandName
="0" Text="首頁" />
<asp:LinkButton ID="lbPreviousPage" runat="server" CommandName="previous" Text="上一頁" /> <asp:LinkButton ID="lbNextPage" runat="server" CommandName="next" Text="下一頁" /> <asp:LinkButton ID="lbLastPage" runat="server" CommandName="last" Text="尾頁" /> 轉至<asp:TextBox ID="tbGotoPage" runat="server" SkinID="Page" MaxLength="5" AutoPostBack="true" /></div>

…………………………………..這之後是關鍵的aspx.cs檔案

const int PageSize = 10;//定義每頁顯示記錄數量 
int _TotalPage, _TotalRecord, _CurrentPage, _Pages, _JumpPage;
                //定義幾個儲存分頁引數變數:總頁數、總記錄數、當前頁碼、頁碼數、跳轉頁碼 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
        _TotalRecord = Calculate();//通過Calculate()函式獲取總記錄數 
        _TotalPage = _TotalRecord / PageSize + OverPage();
                //計算總頁數(加上OverPage()函式防止有餘數造成顯示資料不完整) 
        ViewState["PageCounts"] = _TotalRecord / PageSize - ModPage();//儲存總頁引數到ViewState
                (減去ModPage()函式防止SQL語句執行時溢位查詢範圍,可以用儲存過程分頁演算法來理解這句) 
        ViewState["PageIndex"] = 0;//儲存一個為0的頁面索引值到ViewState 
        ViewState["JumpPages"] = _TotalPage;//儲存_TotalPage到ViewState,跳頁時判斷使用者輸入數是否超出頁碼範圍 
        //顯示lbTotalPage、lbTotalRecord的狀態 
        lbTotalPage.Text = _TotalPage.ToString(); 
        lbTotalRecord.Text = _TotalRecord.ToString(); 
        //判斷跳頁文字框失效 
        if (_TotalRecord <= PageSize) 
        { 
            tbGotoPage.Enabled = false; 
        } 
        BindStatisticsData(); 
    } 
} 
//計算餘頁 
public int OverPage() 
{ 
    int iPages = 0; 
    if (_TotalRecord % PageSize != 0) 
        iPages = 1; 
    else 
        iPages = 0; 
    return iPages; 
} 
//計算餘頁(防止SQL語句執行時溢位查詢範圍) 
public int ModPage() 
{ 
    int iPages = 0; 
    if (_TotalRecord % PageSize == 0 && _TotalRecord != 0) 
        iPages = 1; 
    else 
        iPages = 0; 
    return iPages; 
} 
//計算總記錄數 
public static int Calculate() 
{ 
    int iRecordCount = 0; 
    StatisticsManageInfo StatisticsManageInfo = new StatisticsManageInfo(); 
    SqlDataReader dr = StatisticsManageInfo.Visitor_GetAll(); 
    if(dr.Read()) 
        iRecordCount = Int32.Parse(dr["Total"].ToString()); 
    dr.Close(); 
    return iRecordCount; 
} 
//跳頁程式碼 
private void GotoPage_TextChanged(object sender, EventArgs e) 
{ 
    try 
    { 
        _JumpPage = (int)ViewState["JumpPages"];//從ViewState中讀取可用頁數值儲存到_JumpPage變數值 
        //判斷使用者輸入值是否超過可用頁數範圍值 
        if (Int32.Parse(tbGotoPage.Text) > _JumpPage || Int32.Parse(tbGotoPage.Text) < 0) 
        { 
            Response.Write("<script>alert('您所輸入的頁碼範圍超出了總記錄數!');</script>"); 
        } 
        else 
        { 
            int iInputPage = Int32.Parse(tbGotoPage.Text.ToString()) - 1; 
            ViewState["PageIndex"] = iInputPage; 
            BindStatisticsData(); 
        } 
    } 
    catch (Exception ex) 
    { 
        Response.Write("<script>alert('" + ex.Message + "');</script>"); 
    } 
} 
//對四個按鈕(首頁、上一頁、下一頁、尾頁)返回的CommandName值進行操作 
private void Page_OnClick(object sender, CommandEventArgs e) 
{ 
    _CurrentPage = (int)ViewState["PageIndex"];//從ViewState中讀取頁碼值儲存到CurrentPage變數中進行引數運算 
    _Pages = (int)ViewState["PageCounts"];//從ViewState中讀取總頁引數運算 
    String Cmd = e.CommandName; 
    switch (Cmd) 
    { 
        case"next": 
            _CurrentPage++; 
            break; 
        case "previous": 
            _CurrentPage--; 
            break; 
        case "last": 
            _CurrentPage = _Pages; 
            break; 
        default: 
            _CurrentPage = 0; 
            break; 
    } 
    ViewState["PageIndex"] = _CurrentPage;//將運算後的CurrentPage變數再次儲存至ViewState 
    BindStatisticsData(); 
} 
private void BindStatisticsData() 
{ 
    _CurrentPage = (int)ViewState["PageIndex"];//從ViewState中讀取頁碼值儲存到CurrentPage變數中進行按鈕失效運算 
    _Pages = (int)ViewState["PageCounts"];//從ViewState中讀取總頁引數進行按鈕失效運算 
    if (_CurrentPage + 1 > 1) 
    { 
        lbFirstPage.Enabled = true; 
        lbPreviousPage.Enabled = true; 
    } 
    else 
    { 
        lbFirstPage.Enabled = false; 
        lbPreviousPage.Enabled = false; 
    } 
    if (_CurrentPage == _Pages) 
    { 
        lbNextPage.Enabled = false; 
        lbLastPage.Enabled = false; 
    } 
    else 
    { 
        lbNextPage.Enabled = true; 
        lbLastPage.Enabled = true; 
    } 
    //以下是從資料庫獲取表資料,並繫結到GridView控制元件,下面我用了SQLHelper來完成,通過儲存過程呼叫資料庫資料。 
    StatisticsManageInfo StatisticsManageInfo = new StatisticsManageInfo(); 
    SqlDataReader dr = StatisticsManageInfo.Visitor_GetVisitor(PageSize,_CurrentPage); 
    gvVisitor.DataSource = dr; 
    gvVisitor.DataBind(); 

    lbCurrentPage.Text = (_CurrentPage + 1).ToString(); 
    tbGotoPage.Text = (_CurrentPage + 1).ToString(); 
} 
//以下部分,是初始化控制元件的,分頁程式碼原本沒有這一段,但是執行不了,所以後來我加上的。原因不明,待研究。 
#region Web 窗體設計器生成的程式碼 
override protected void OnInit(EventArgs e) 
{ 
    InitializeComponent(); 
    base.OnInit(e); 
} 
private void InitializeComponent() 
{ 
    this.lbFirstPage.Command += new CommandEventHandler(this.Page_OnClick); 
    this.lbLastPage.Command += new CommandEventHandler(this.Page_OnClick); 
    this.lbNextPage.Command += new CommandEventHandler(this.Page_OnClick); 
    this.lbPreviousPage.Command += new CommandEventHandler(this.Page_OnClick); 
    this.tbGotoPage.TextChanged += new System.EventHandler(this.GotoPage_TextChanged); 
    this.Load += new System.EventHandler(this.Page_Load); 
} 
#endregion