gridview自定义翻页

前台代码:(GridView部分 

<asp:GridView ID="GridView1" 
runat="server"
 
AutoGenerateColumns="False"
 
BorderStyle="None"
 
AllowPaging="True"
 
AllowSorting="True"
 
CellPadding="5"
OnPageIndexChanging="GridView1_PageIndexChanging"
 
OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand"   
 
>
 
              <Columns>
                       <asp:BoundField DataField="nd" HeaderText="
年代"></asp:BoundField>
                        <asp:BoundField DataField="zz" HeaderText="
作者"></asp:BoundField>
              </Columns>
              <PagerSettings Visible="False" />                                

  这句是让GridView中原有的分页功能不起作用                            
 </asp:GridView>

GridView前台代码后跟着的分页控件前台代码
<asp:LinkButton ID="btnFirst" runat="server" OnClick="PagerButtonClick">
首页</asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" OnClick="PagerButtonClick">
上一页</asp:LinkButton>
<asp:Label ID="LblCurrentIndex" runat="server" Text="Label"></asp:Label>
<asp:Label ID="LblPageCount" runat="server" Text="Label"></asp:Label>
<asp:Label ID="LblRecordCount" runat="server" Text="Label"></asp:Label>
 <asp:LinkButton ID="btnNext" runat="server" OnClick="PagerButtonClick">
下一页</asp:LinkButton>
 <asp:LinkButton ID="btnLast" runat="server" OnClick="PagerButtonClick">
末页</asp:LinkButton>
跳转到第<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCurrentPage_SelectedIndexChanged"  Width="40px">
 </asp:DropDownList>

后台代码
BindData
函数是后台数据绑定函数。需要在页面载入的时候调用
 public void BindData()

    {

        DataTable dd = new DataTable();
                dd
为数据源,可以通过数据库获取,此处填写你的SQL操作 

       GridView1.DataSource = dd;

        GridView1.DataKeyNames = new string[] { "ID" };//主键 

        GridView1.DataBind();

 

        LblCurrentIndex.Text = " " + (GridView1.PageIndex + 1).ToString() + " ";

        LblPageCount.Text = " " + GridView1.PageCount.ToString() + " ";

        int intReCount = dd.Rows.Count;//记录集数

        //LblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " ";

        LblRecordCount.Text = "总共 " + intReCount.ToString() + " ";

        // this.LblRecordCount.Text = string.Format("当前第{0}/总共{1}", this.GridView1.PageIndex + 1, this.GridView1.PageCount);

 if (intReCount == 0)

        {

            btnFirst.Visible = false;

            btnPrev.Visible = false;

            btnNext.Visible = false;

            btnLast.Visible = false;

 

            LblCurrentIndex.Visible = false;

            LblPageCount.Visible = false;

            LblRecordCount.Visible = false;

        }

        else if (GridView1.PageCount == 1)

        {

            btnFirst.Visible = false;

            btnPrev.Visible = false;

            btnNext.Visible = false;

            btnLast.Visible = false;

        } 

        btnFirst.CommandName = "1";

        btnPrev.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());

        btnNext.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());

        btnLast.CommandName = GridView1.PageCount.ToString(); 

        //========================================================

        //跳页

 this.ddlCurrentPage.Items.Clear();

        for (int i = 1; i <= this.GridView1.PageCount; i++)

        {

            this.ddlCurrentPage.Items.Add(i.ToString());

        }

        //把当前页数给DropDownList

        this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;

        //跳页 end ========================================================  

    } 

    protected void PagerButtonClick(object sender, EventArgs e)

    {

        GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;

        BindData();

 

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        //查询后,翻页问题的解决办法绑定之前,

        //加入一语句:GridView1.PageIndex = e.NewPageIndex 

        GridView1.PageIndex = e.NewPageIndex;

        BindData(); 

    }

    protected void ddlCurrentPage_SelectedIndexChanged(object sender, EventArgs e)

    {

        //跳页,显示数据 

        this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;

   BindData();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值