zoukankan      html  css  js  c++  java
  • 分页技术之GridView控件

    GridView控件实现分页技术

    第一步:设置GridView控件的属性,跟分页相关的属性设置如下:

    AllowPaging="true":允许分页, PageSize="大小":每一页显示的信息条数,

    OnPageIndexChanging="GridView1_PageIndexChanging":页数改变时触发的事件。

    第二步:设置GridView控件的PagerTemplate页模板属性。PageTemplate是用来获取或设置 GridView 控件中页导航行的自定义内容。代码实现如下:

     1 <PagerTemplate>
     2 
     3 <table width="100%">
     4 
     5    <tr>
     6 
     7       <td style="text-align: center">
     8 
     9  
    10 
    11            第<asp:Label  ID="lblPageIndex"  runat="server"
    12 
    13           Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>" ></asp:Label>14 
    15  
    16 
    17          共<asp:Label  ID="lblPageCount"  runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>18 
    19  
    20 
    21           <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First"
    22 CommandName="Page" Text="首页"></asp:LinkButton>
    23 
    24  
    25 
    26           <asp:LinkButton ID="btnPrev" runat="server"
    27 CausesValidation="False" CommandArgument="Prev"
    28 CommandName="Page" Text="上一页"></asp:LinkButton>
    29 
    30  
    31 
    32           <asp:LinkButton ID="btnNext" runat="server"
    33 CausesValidation="False" CommandArgument="Next"
    34 CommandName="Page" Text="下一页"></asp:LinkButton>
    35 
    36  
    37 
    38           <asp:LinkButton ID="btnLast" runat="server"
    39 CausesValidation="False" CommandArgument="Last"
    40 CommandName="Page" Text="尾页"></asp:LinkButton>
    41 
    42  
    43 
    44 <asp:TextBox  ID="txtNewPageIndex"  runat="server"
    45 Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1%>" Width="20px"></asp:TextBox>
    46 
    47                            
    48 
    49 <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1"  CommandName="Page" Text="GO"></asp:LinkButton>
    50 
    51           
    52 
    53 </td>
    54 
    55 </tr>
    56 
    57 </table>
    58 
    59 </PagerTemplate>
    View Code

    ((GridView)Container.Parent.Parent).PageIndex + 1获取当前控件显示的页面数;((GridView)Container.Parent.Parent).PageCount获取当前控件总的页面数;CausesValidation设置按钮提交时不执行验证;CommandArgument设置与关联的 CommandName 属性一起传递到 Command 事件处理程序的可选参数;CommandName设置命令名,该命令名与传递给 Command 事件的 Button 控件相关联。

    第三步:后台GridView1_PageIndexChanging事件的实现,代码如下:

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //通过类型转换得到当前的gridview控件
            GridView gvw = (GridView)sender;
     
    
            if (e.NewPageIndex < 0)
            {
                TextBox PageNum = (TextBox)GridView1.BottomPagerRow.FindControl("txtNewPageIndex");
    
                int Pa = int.Parse(PageNum.Text);
    
                if (Pa <= 0)//如果前往分页的index小于或等于0则转向0
                {
                    gvw.PageIndex = 0;
                }
                else
                {
                    gvw.PageIndex = Pa - 1;
                }
            }
            else
            {
                gvw.PageIndex = e.NewPageIndex;
            } 
    
            bind();//自定义的GridView控件绑定数据函数
        }
    

      *小弟个人拙见,各位仁兄有更好的方法请多多指教。

  • 相关阅读:
    在Fiddler中捕获IIS / ASP.NET流量
    SQL Server 索引分析开关
    解决谷歌浏览器在F12情况下自动断点问题(Paused in debugger)
    SQL Server 移位运算符
    手动更新了packages.config Nuget配置文件,自动引用dll
    转载:如何严格限制session在30分钟后过期!
    Sphinx 2.0.8 发布,全文搜索引擎 Installing Sphinx on Windows
    Where does Oracle SQL Developer store connections? oracle SQL Developer 连接信息保存的位置,什么地方
    利用 ROW_NUMBER() OVER ( ORDER BY 进行选择性排序,按不同字段进行排序处理,分页
    大数据学习网站
  • 原文地址:https://www.cnblogs.com/lhm814/p/3649371.html
Copyright © 2011-2022 走看看