c#分页代码

本来不想写这个的,呵呵。但是感觉看到了,还是留一下吧,没事看看可以拓展下思路。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView runat="server" ID="Grid1"></asp:GridView>
        <br />
        <asp:LinkButton ID="btn1" runat="server" CommandName="aa"  OnCommand="btnOK">[首  页]</asp:LinkButton>
        <asp:LinkButton ID="btn2" runat="server" CommandName="bb"  OnCommand="btnOK">[上一页]</asp:LinkButton>
        <asp:LinkButton ID="btn3" runat="server" CommandName="cc"  OnCommand="btnOK">[下一页]</asp:LinkButton>
        <asp:LinkButton ID="btn4" runat="server" CommandName="dd"  OnCommand="btnOK">[末  页]</asp:LinkButton>
    </div>
    </form>
</body>
</html>

C#

    static int pageIndex = 1;//当前的页面
    static int pages = 0;    //总页数
    static int pageSize = 10;//当前页的数据条数
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SelectCount();
            Grid();
        }
       
    }

    protected string conString = "server=.;database=DB;uid=sa;pwd=sa2010";

    //计算出总行数和总页数
    protected void SelectCount()
    {
        int count = 0;
        using (SqlConnection con = new SqlConnection(conString))
        {
            con.Open();
            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandText = "select count(*) from Test";
                count = (int)cmd.ExecuteScalar();
            }
        }
        //计算总页数
        if (count % pageSize == 0)
        {
            pages = count / pageSize;
        }
        else
        {
            pages = (count / pageSize) + 1;
        }
    }

    protected void Grid()
    {
        int start = (pageIndex - 1) * pageSize;
        SqlConnection cod = new SqlConnection(conString);
        string sql = "select * from Test";
        SqlDataAdapter da = new SqlDataAdapter(sql,cod);
        DataSet ds = new DataSet();
        da.Fill(ds, start, pageSize, "Test");
        Grid1.DataSource = ds;
        Grid1.DataBind();
    }

    protected void btnOK(object o, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "aa":
                pageIndex = 1;
                Grid();
                break;
            case "bb":
                if (pageIndex > 1)
                {
                    pageIndex--;
                    Grid();
                }
                break;
            case "cc":
                if (pageIndex < pages)
                {
                    pageIndex++;
                    Grid();
                }
                break;
            case "dd":
                pageIndex = pages;
                Grid();
                break;
        }
    }

下边是我改写的哈,没测试效率,有朋友有时间可以测试下。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>

<!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 style="text-align:center;">

            <asp:LinkButton ID="btn2" runat="server" CommandName="bb" OnCommand="btnOK">[上一页]</asp:LinkButton>
            <asp:LinkButton ID="btn3" runat="server" CommandName="cc" OnCommand="btnOK">[下一页]</asp:LinkButton>
       
            <table border="1" cellpadding="0" cellspacing="0">
                <asp:Repeater ID="News" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem,"newsid") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem,"newsname") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem,"adddate") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem,"addname") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem,"viewnum") %>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
                <asp:HiddenField ID="Start" runat="server" />
                <asp:HiddenField ID="End" runat="server" />
            </table>
        </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 Conn;
using System.Data.OleDb;

public partial class Index : System.Web.UI.Page
{
    public int NewsNumber = 0;
    public int StartID = 0;
    public int EndID = 0;
    public int PageCount = 100;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindNews(StartID, PageCount);
        }
    }
    public int NewsCount()
    {
        int NewsNumber = 0;
        object ob;
        ob = SqlConn.GetSingle("Select Count(*) from News");
        NewsNumber = Convert.ToInt32(ob);
        return NewsNumber;
    }

    public void BindNews(int StartID, int PageCount)
    {
        DataSet Ds = new DataSet();
        if (StartID == 0)
        {
            Ds = SqlConn.Query("Select Top " + PageCount + " * From News Order by NewsID desc");
        }
        else
        {
            Ds = SqlConn.Query("Select Top " + PageCount + " * From News Where NewsID < " + StartID + " Order by NewsID Desc");
        }
        StartID = Convert.ToInt32(Ds.Tables[0].Rows[Ds.Tables[0].Rows.Count - 1]["NewsID"]);
        EndID = Convert.ToInt32(Ds.Tables[0].Rows[0]["NewsID"]);
        this.Start.Value = StartID.ToString();
        this.End.Value = EndID.ToString();

        this.News.DataSource = Ds;
        this.News.DataBind();
    }
    protected void btnOK(object o, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "bb":
                BindNews(Convert.ToInt32(this.End.Value), PageCount);
                break;
            case "cc":
                BindNews(Convert.ToInt32(this.Start.Value), PageCount);
                break;

        }
    }
}

posted @ 2011-04-28 14:51  惊蛰.  阅读(1422)  评论(0编辑  收藏  举报