GridView 手动分页代码

本文介绍了一个自定义的ASP.NET GridView分页功能实现案例,通过源码展示了如何使用LinkButton和Label控件来自定义分页模板,并在后台代码中处理页面索引改变的事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//源码   

<PagerTemplate>
                          <asp:LinkButton ID="lbFirst" runat="server" CausesValidation="False" CommandArgument="First" CommandName="Page">首页</asp:LinkButton>
                        <asp:LinkButton ID="lbPrev" runat="server" CausesValidation="False" CommandArgument="Prev" CommandName="Page">上一页</asp:LinkButton>
                        <asp:LinkButton ID="lbNext" runat="server" CausesValidation="False" CommandArgument="Next" CommandName="Page">下一页</asp:LinkButton>
                        <asp:LinkButton ID="lbLast" runat="server" CausesValidation="False" CommandArgument="Last" CommandName="Page">尾页</asp:LinkButton> 第<asp:Label ID="Label2" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>"></asp:Label>页              共<asp:Label ID="Label1" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页
                        跳到<asp:TextBox ID="tbPage" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>" Width="27px"></asp:TextBox>
                       <asp:LinkButton ID="lbGO" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO"></asp:LinkButton>
</PagerTemplate>
                        </asp:GridView>

 

 

//后台

protected void GridView_LYBX_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        GridView gvw = (GridView)sender;
        if (e.NewPageIndex < 0)
        {
            TextBox pageNum = (TextBox)gvw.BottomPagerRow.FindControl("tbPage");
            int Pa = int.Parse(pageNum.Text);
            if (Pa <= 0)
                gvw.PageIndex = 0;
            else
                gvw.PageIndex = Pa - 1;
        }
        else
        {
            gvw.PageIndex = e.NewPageIndex;
        }
        string strSQL = string.Format("select * from tb_goodnews");
        databand(GridView_LYBX, strSQL);
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值