gridview 手写分页

前台  各种按钮

<div id="aaa" runat="server">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr align="center">
<td style="width: 670px> 

共<asp:Label ID="lblPageCount" runat="server" ForeColor="Red"></asp:Label>

页, 当前第<asp:Label ID="lblCurrPage" runat="server" ForeColor="Red"></asp:Label>

页&nbsp;&nbsp; 共<asp:Label ID="lblTotalCount" runat="server" ForeColor="Red"></asp:Label>

条记录, 每页显示<asp:Label ID="lblPageSize" runat="server" Text="5" ForeColor="Red"></asp:Label>条&nbsp;&nbsp;
</td>
</tr>
<tr align="center">
<td style="width: 670px>                                               

<asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click1" >首页</asp:LinkButton>&nbsp;
<asp:LinkButton ID="btnPrev" runat="server" OnClick="btnPrev_Click1" >上一页</asp:LinkButton>&nbsp;
<asp:LinkButton ID="btnNext" runat="server" OnClick="btnNext_Click1" >下一页</asp:LinkButton>&nbsp;
<asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click1" >尾页</asp:LinkButton>

&nbsp;
跳到<asp:DropDownList ID="ddlPager" runat="server" Width="50"  OnSelectedIndexChanged="asd_SelectedIndexChanged" />
 
</td>
</tr>
</table>

</div>

 

 

后台

protected void Page_Load(object sender, EventArgs e)
    {

        this.NoLogin();
        //GridViewBind();
        if (!IsPostBack)
        {
            //GridView1.DataBind();
            GridViewBind("1");
        }
    }

    protected void btnFirst_Click1(object sender, EventArgs e)
    {
        GridViewBind("1");
    }
    protected void btnPrev_Click1(object sender, EventArgs e)
    {
        GridViewBind("-1");
    }
    protected void btnNext_Click1(object sender, EventArgs e)
    {
        GridViewBind("+1");
    }
    protected void btnLast_Click1(object sender, EventArgs e)
    {
        GridViewBind("0");
    }
    protected void asd_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewBind(ddlPager.Text);

        ddlPager.ClearSelection();
    }
    //绑定
    public void GridViewBind(string page)
    {
      
        string sqlstr = "select *from D_HOUSE_SUBSCRIBE_REQUEST where D_HOUSE_SUBSCRIBE_REQUEST.AUDITING_STATE=0";
        DataSet ds = SqlHelper.ExecuteDataSet(sqlstr);
        int a = ds.Tables.Count;
        int b = ds.Tables[0].Rows.Count;
        if (ds == null || ds.Tables[0].Rows.Count <= 0)
        {
            int sr = 1;
            return;
        }
        else
        {
           
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            int totalCount = dv.Count;//总条数
            int pageSize = 2;//每页显示条数
            int pageCount = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize + 1);//总页数
            int currPage; //当前页数
            switch (page)
            {
                //首页
                case "1":
                    currPage = 1;
                    break;
                //下一页
                case "+1":
                    currPage = int.Parse(lblCurrPage.Text) + 1;
                    break;
                //上一页
                case "-1":
                    currPage = int.Parse(lblCurrPage.Text) - 1;
                    break;
                //尾页
                case "0":
                    currPage = pageCount;
                    break;
                //下拉列表框直接传值时
                default:
                    currPage = int.Parse(page);
                    break;
            }
            if (!IsPostBack)
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    ddlPager.Items.Add(i.ToString());
                }
            }
            //设置分页信息的显示
            lblCurrPage.Text = currPage.ToString();
            lblPageCount.Text = pageCount.ToString();
            lblPageSize.Text = pageSize.ToString();
            lblTotalCount.Text = totalCount.ToString();
            //设置翻页按钮的可用性
            btnFirst.Enabled = btnPrev.Enabled = btnNext.Enabled = btnLast.Enabled = true;
            if (currPage == 1)
            {
                btnFirst.Enabled = btnPrev.Enabled = false;
            }
            if (currPage == pageCount)
            {
                btnNext.Enabled = btnLast.Enabled = false;
            }
            ddlPager.Text = currPage.ToString();
            //从查询出的所有记录中筛选出即将呈现的当前页的数据集合
            int loopCount = (currPage == pageCount) ? totalCount - (currPage - 1) * pageSize : pageSize; //显示的记录条数
            DataRowView[] drwsToBind = new DataRowView[loopCount];
            for (int i = 0; i < loopCount; i++)
            {
                drwsToBind[i] = dv[(currPage - 1) * pageSize + i];
            }
            GridView1.DataSource = drwsToBind;
            GridView1.DataBind();
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值