GridView 使用存储过程分页

 

 

存储过程

use blog

go

create procedure blogcontent_page

@pageSize int,   --一页多少条

@pageIndex int,  --第几页

@pageCount int output  --一共多少页,输出参数

as

begin

declare @totalRecords int --定义变量记录条数

select @totalRecords = count(id) from blog_content--取出总记录条数

set @pageCount=ceiling(@totalRecords / (@pageSize+0.0));--取出总页数

with temp as (select row_number() over (order by id) as inde, * from blog_content)  --给这个表再添加一列序号

select * from temp where inde between (@pageIndex - 1)* @pageSize+1 and @pageIndex * @pageSize  --按页数查询

return @totalRecords

end

 


 

后台代码

private void FullGridView(int index)

    {

        string sql = ConfigurationManager.ConnectionStrings["sqlblog"].ConnectionString;

        using (SqlConnection conn = new SqlConnection(sql))

        {

            using (SqlCommand comm = conn.CreateCommand())

            {

                comm.CommandText = "blogcontent_page";

                comm.CommandType = System.Data.CommandType.StoredProcedure;

                comm.Parameters.AddWithValue("@pageSize", 3);

                comm.Parameters.Add("@pageCount", SqlDbType.Int).Direction = ParameterDirection.Output;

                comm.Parameters.AddWithValue("@pageIndex", index);

                SqlDataAdapter da = new SqlDataAdapter(comm);

                DataSet ds = new DataSet();

                da.Fill(ds);

                this.GridView1.DataSource = ds.Tables[0];

                this.GridView1.DataBind();

                this.HiddenField1.Value = index.ToString();//当前页数

                this.HiddenField2.Value = comm.Parameters["@pageCount"].Value.ToString();//总共的页数

                this.Label4.Text = index.ToString()+"/"+HiddenField2.Value;

            }

        }

    }

protected void Button1_Click(object sender, EventArgs e)

    {

        FullGridView (1);

    }

    //最后一页

    protected void Button4_Click(object sender, EventArgs e)

    {

        FullGridView (Convert.ToInt32(this.HiddenField2.Value));

    }

    //上一页

    protected void Button2_Click(object sender, EventArgs e)

    {

        int index =Convert.ToInt32 (this.HiddenField1.Value);

        if (index>1)

        {

            FullGridView (index - 1);

        }

    }

    //下一页

    protected void Button3_Click(object sender, EventArgs e)

    {

        int index = Convert.ToInt32(this.HiddenField1.Value);

        if (index < Convert.ToInt32(this.HiddenField2.Value))

        {

            FullGridView (index + 1);

        }

    }

    //跳到多少页

    protected void Button5_Click(object sender, EventArgs e)

    {

        int countpage=Convert.ToInt32(this.HiddenField2.Value);

        int index = Convert.ToInt32(this.TextBox1.Text);

        if (index>0&&index<=countpage)

        {

            FullGridView (index);

        }

    }


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值