ASP分页处理(1)

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default-自定义.aspx.cs" Inherits="Default_自定义" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/First.gif" OnClick="ImageButton1_Click" />
        <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Images/Next.gif" OnClick="ImageButton2_Click" style="height: 22px" />
        <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/Images/Previous.gif" OnClick="ImageButton3_Click" />
        <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="~/Images/Last.gif" OnClick="ImageButton4_Click" style="height: 22px" />
        <br />
    </form>
</body>
</html>





后台代码:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class Default_自定义 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
            //功能:现在要做一个分页的功能。
            //思路:1.设置页面大小和页面索引
            //          2.写数据库代码,根据数据库的索引,查出数据
            //          3.判断总共有几页
            ViewState["PageIndex"] = 0;
            ViewState["PageSize"] = 5;  
            connect();
        }
    }


    /// <summary>
    /// 数据绑定的代码
    /// </summary>
    private void connect()
    {
        string s = ViewState["PageIndex"].ToString();
        string ss = ViewState["PageSize"].ToString();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strcon"].ConnectionString);
        string sql = string.Format(@"select top {0} * from Admin where AdminId not in (select top {1} AdminId from Admin)",(int)ViewState["PageSize"],(int)ViewState["PageIndex"]*(int)ViewState["PageSize"]);
     //   string sql = @"select top 5 * from admin where AdminID not in
//(select top 0 AdminID from  admin)";


        SqlDataAdapter da = new SqlDataAdapter(sql, con);
       


        DataSet ds = new DataSet();
        da.Fill(ds);


        string sql2 = "select count(*) from Admin";
        SqlDataAdapter da2 = new SqlDataAdapter(sql2, con);
        DataSet ds2 = new DataSet();
        da2.Fill(ds2);
       


        //计算出到底有多少页,方便后面好做判断
        //取出datatable对象
        //这里的rows[0][0]搞不懂
        ViewState["PageCount"] = (int)ds2.Tables[0].Rows[0][0] % (int)ViewState["PageSize"] == 0 ? (int)ds2.Tables[0].Rows[0][0] / (int)ViewState["PageSize"] : (int)ds2.Tables[0].Rows[0][0] / (int)ViewState["PageSize"] + 1;
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //第一页:
        ViewState["PageIndex"] = 0;
        connect();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        //下一页
        if ((int)ViewState["PageIndex"] < (int)ViewState["PageCount"]-1)
        {
            int i=(int)ViewState["PageIndex"];
            int j = (int)ViewState["PageCount"];
            ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
            connect();
        }
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        //向上翻一页
        if ((int)ViewState["PageIndex"] >0)
        {
            ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
            connect();
        }
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"]-1;
        connect();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值