ASP.NET GridView控件实现分页跳转功能

最近刚刚接触ASP.NET开发,自己做一个小网站,用到了GridView控件的分页,但是触发不了IndexPageChanging事件,查找了很多资料,好不容易解决了,写一下我的使用心得;

注意:源码是借鉴别人的案例 点击打开链接

开发环境:

VS2010

前期准备:

1.更改GridView控件属性AllowPaging为true;

2.根据自定义需求更改控件属性PageSize;

3.更改GridView控件属性EnableViewState为true(就是这个属性难住很久);

4.设置GridView控件分页触发事件:onpageindexchanging="VersionList_PageIndexChanging";

前台代码:

在GridView控件末尾添加如下的前端代码

<PagerTemplate>
                当前第:
                <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
                页/共
                <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
                页
                 <asp:Button ID="ButtonFirstPage" runat="server" Text="首页" CommandArgument="First" CommandName="Page" Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>' />                
                <asp:Button ID="ButtonPreviousPage" runat="server" Text="上一页" CommandArgument="Prev" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' />
                <asp:Button ID="LinkButtonNextPage" runat="server" Text="下一页" CommandArgument="Next" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>' />
                <asp:Button ID="LinkButtonLastPage" runat="server" Text="尾页" CommandArgument="Last" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>' />
                转到第
                <asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'/>
                页
                <asp:Button ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2" CommandName="Page" Text="Go"/>
            </PagerTemplate>


后台代码:

编写分页触发事件

 protected void VersionList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // 得到该控件
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;

            //VersionList.PageIndex = e.NewPageIndex;
             //重新绑定
            Bind(sql_Default);
        }

重新生成后就可以完成分页操作了。



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值