程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.NET 2.0 分頁技術之使用純sql語句的雙top分頁篇

ASP.NET 2.0 分頁技術之使用純sql語句的雙top分頁篇

編輯:.NET實例教程

Repeater在前台使用比較靈活自由,但有一個問題就是Repeater不支持直接分頁,這個很多人看起來就有點不想用了,但我想大家都知道GridView控件或DataGrid控件在啟用自帶分頁的時候其實效率是非常低的,大的不說,一但到了百萬級數據以後,就會感覺是多麼的痛苦和無耐了,所以即使是用DataGrid(GridVIEw)控件,高手們還是只會只使用一個界面而已,而分頁肯定都是自己寫的,這樣一比較利弊就出來了,既然這樣那就選擇Repeater吧,本篇示例演示使用純sql語句的雙top分頁法達到控件分頁的功能.
--------------------------------------------------------------------------------

前台代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.ASPx.cs" Inherits="Default3" %>

<!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>無標題頁</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ASP:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <TABLE borderColor=#e5e3e3 cellSpacing=0 cellPadding=3 width=790 align=center border=1>
              <TR bgColor=#fff9ec>
                <TH align="center" width=200 bgColor=#fff9ec>名稱</TH>
                <TH align="center" width=200 bgColor=#fff9ec>類別</TH>
                <TH align="center" width=200 bgColor=#fff9ec>發布時間</TH>
                <TH align="center" width=200 bgColor=#fff9ec>操作</TH>
            

$False$

;  </TR>
        </HeaderTemplate>
        <ItemTemplate>
            <TR>
                <TD align="center" width=200><%#Eval("job_id")%></TD>
                <TD align="center" width=200><%#Eval("job_desc")%></TD>
                <TD align="center" width=200><%#Eval("min_lvl")%></TD>
                <TD align="center" width=200><%#Eval("max_lvl")%></TD>
            </TR>
        </ItemTemplate>
        <FooterTemplate>
            </TABLE>
        </FooterTemplate>
        </ASP:Repeater>
    </div>
    <p></p>
    <div align="center">運行時間:
        <asp:Label ID="Label1" runat="server" Text="Label"></ASP:Label>
        <asp:HyperLink ID="HyperLink1" runat="server">第一頁</ASP:HyperLink>
        <asp:HyperLink ID="HyperLink2" runat="server">上一頁</ASP:HyperLink>
        <asp:HyperLink ID="HyperLink3" runat="server">下一頁</ASP:HyperLink>
        <asp:HyperLink ID="HyperLink4" runat="server">末一頁</ASP:HyperLink>
    </div>
    </form>
</body>
</Html>

後台事件代碼:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClIEnt;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int cpage;
        if (Request.QueryString["page"] != null)
            cpage = Convert.ToInt32(Request.QueryString["page"]);
   else
            cpage = 1;

        TimeSpan newtimespan = DateTime.Now.TimeOfDay;//運行時間開始
        int pagesize = 5;
        string sql = "";
        string sqlstr = "select count(*) from jobs";
        SqlConnection con = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=sa;");
        SqlCommand cmd = new SqlCommand(sqlstr, con);
        con.Open();
        int totalput = Convert.ToInt32(cmd.ExecuteScalar());

        int maxpage = 1;
        if (totalput % pagesize == 0)
        {
            maxpage = totalput / pagesize;
        }


        else
        {
            maxpage = totalput / pagesize + 1;
        }
        if (maxpage == 0) { maxpage = 1; }
        if (cpage < 1) { cpage = 1; }
        else if (cpage > maxpage) { cpage = maxpage; }

        if (totalput != 0)
        {
            if (cpage == 1)
            {
                sql = "select top " + pagesize + " * from jobs order by job_id desc";
            }
            else
            {
                sql = "select top " + pagesize + " * from jobs where job_id not in(select top " + (cpage - 1) * pagesize + " job_id from jobs order by job_id desc) order by job_id desc";
            }
        }

        SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        adapter.Dispose();


        this.Label1.Text = "共有信息" + totalput.ToString() + "條  當前是第" + cpage.ToString() + "/" + maxpage.ToString() + "頁  ";
        if (cpage != 1)


        {
            this.HyperLink1.NavigateUrl = "Default3.ASPx?page=1";
            this.HyperLink2.NavigateUrl = "Default3.ASPx?page=" + Convert.ToString(cpage - 1);
        }
        if (cpage != maxpage)
        {
            this.HyperLink3.NavigateUrl = "Default3.ASPx?page=" + Convert.ToString(cpage + 1);
            this.HyperLink4.NavigateUrl = "Default3.ASPx?page=" + maxpage.ToString();
        }

        this.Repeater1.DataSource = ds.Tables[0].DefaultVIEw;
        this.Repeater1.DataBind();
        //算出運行時間並附值給Label控件顯示
        this.Label1.Text = DateTime.Now.TimeOfDay.Subtract(newtimespan).TotalMilliseconds.ToString();
    }
}

如上述代碼有什麼問題請留言或聯系我!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved