ajax实现分页

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <div>
   
        <asp:GridView ID="GridView1" runat="server" Height="167px" Width="1000px"
            AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="TITLE" HeaderText="标题" />
                <asp:BoundField DataField="CLASSNAME" HeaderText="类别" />
                <asp:BoundField DataField="CLICKCOUNT" HeaderText="点击量" />
                <asp:BoundField DataField="PUBLISHER" HeaderText="作者" />
                <asp:BoundField DataField="PUBLISHED" HeaderText="出版社" />
            </Columns>
        </asp:GridView>
        <br />
        <br />
        <asp:Button ID="btnfirst" runat="server" Text="第一页" οnclick="btnfirst_Click"
            style="height: 21px" />
        <asp:Button ID="btnpre" runat="server" Text="上一页" οnclick="btnpre_Click" />
        <asp:Button ID="btnnext" runat="server" Text="下一页" οnclick="btnnext_Click" />
        <asp:TextBox ID="txtpageindex" runat="server"></asp:TextBox>
        <asp:Button ID="btnGO" runat="server" Text="GO" οnclick="btnGO_Click" />
   
        <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="最后一页" />
   
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>

 

 

string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
        private int pagesize = 5;
        private int totalpagesize=0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["pageindex"] = "1";
                this.txtpageindex.Text = "1";
                DataBindArticle();
            }
           
        }
        private void DataBindArticle()
        {
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "PROFENYE";
            //表明调用的是存储过程
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PAGESIZE",pagesize);
            cmd.Parameters.AddWithValue("@PAGEINDEX", Convert.ToInt32(ViewState["pageindex"]));
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }
        //获取总的页数
        private void GetCount()
        {
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select count(*) from ArticleInfo";
            conn.Open();
            object obj = cmd.ExecuteScalar();
            if (obj != null)
            {
                int totalrecord = Convert.ToInt32(obj);
                if (totalrecord % pagesize != 0)
                {
                    totalpagesize = totalrecord / pagesize + 1;
                }
                else
                {
                    totalpagesize = totalrecord / pagesize;
                }
            }
            cmd.Dispose();
            conn.Dispose();
        }


        protected void btnfirst_Click(object sender, EventArgs e)
        {
            ViewState["pageindex"] = "1";
            this.txtpageindex.Text = "1";
            DataBindArticle();
        }

        protected void btnpre_Click(object sender, EventArgs e)
        {
            int pageindex=Convert.ToInt32(ViewState["pageindex"]);
            if (pageindex > 1)
            {
                pageindex--;
                ViewState["pageindex"] = pageindex.ToString();
                this.txtpageindex.Text = pageindex.ToString();
            }
            DataBindArticle();
        }

        protected void btnnext_Click(object sender, EventArgs e)
        {
            int pageindex = Convert.ToInt32(ViewState["pageindex"]);
            GetCount();
            if (pageindex < totalpagesize)
            {
                pageindex++;
                ViewState["pageindex"] = pageindex.ToString();
                this.txtpageindex.Text = pageindex.ToString();
            }
            DataBindArticle();
        }

      

        protected void Button1_Click(object sender, EventArgs e)
        {
            GetCount();
            ViewState["pageindex"] = totalpagesize.ToString();
            this.txtpageindex.Text = totalpagesize.ToString();
            DataBindArticle();
        }

        protected void btnGO_Click(object sender, EventArgs e)
        {
            ViewState["pageindex"] = this.txtpageindex.Text;
            DataBindArticle();
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值