tax

创新源自善于发现

导航

gridview分页加入go跳转

【前言】:一直潜伏在这个园子里很久了,总是默默地关注着这里的一些细小的改变,看着很多精湛、高深的博文、博主,让我暗自钦佩。今天试着开通了自己的博客,但却不知道该发表些什么样的博文好点。很多观点、很多解说都被疯狂的转载,可能在这里的一篇博文就出现了很多地方的影子。。。犹豫着决定把在开发过程中遇到的问题和解决问题的方法分享出来,给自己一个积累的空间,一点一滴的记录那个曾经的过程!

 

【问题】:在用gridview绑定数据的时候,由于数据量比较大了,对分页来说一页一页的分太不理想了,于是决定加入一个选择页面然后在进行跳转,搜了很多资料解说的比较散,今天测试成功了拿来记录一下。

 

GridView较之DataGrid提供了更加强大、更加完善的功能,而且具备了丰富的可扩展功能,可以使用GridView提供的pagertemplate自定义分页模板:

事实上,GridView默认的几中分页样式,都是将相关按钮的CommandName设为Page,而CommandArgument设为相关参数,可接受的参数包括,first,last,prev,next,<PageIndex>(具体数字),然后按事件回溯,触发顶层的RowCommand,因此我们页可以使用这些默认的可识别的参数自定义自己的分页模板,asp.net会自动设置当前的NewPageIndex,而不需要任何的冗余代码。

.aspx页面:


<asp:gridview id="GridView1" runat="server" allowpaging="True" pagesize="10" autogeneratecolumns="False"   onpageindexchanging="GridView1_PageIndexChanging">
             <columns>
                 <asp:boundfield datafield="CompanyName" headertext="CompanyName" sortexpression="CompanyName" />
                 <asp:boundfield datafield="ContactTitle" headertext="ContactTitle" sortexpression="ContactTitle" />
                 <asp:boundfield datafield="Phone" headertext="Phone" sortexpression="Phone" />
                 <asp:boundfield datafield="Fax" headertext="Fax" sortexpression="Fax" />
                 <asp:boundfield datafield="ContactName" headertext="ContactName" sortexpression="ContactName" />
             </columns>
                        <pagertemplate>
                         <table width="100%">
                           <tr>
                             <td style="text-align:right">
                             第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1   %>' />页
                                 共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount   %>' />页
                                 <asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
                               <asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
                              <asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />                         
                              <asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />                                           
                              <asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1   %>' />
                              <asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
                              </td>
                           </tr>
                         </table>
                     </pagertemplate>
         </asp:gridview> 
    
    
PageIndexChanging处理程序:

  1.  protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                GridView theGrid = sender as GridView; 
                int newPageIndex = 0;
                if (-2 == e.NewPageIndex)
                {
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = theGrid.BottomPagerRow;
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;  
                    }
                    if (null != txtNewPageIndex)
                    {
                        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;
            }


   注意到,上面的示例中,由于增加了一个跳转按钮GO,但是asp.net不支持相关的CommandArgument值,虽然可以将Go Button的Commandname设为Page,还需要手动的在PageIndexChanging增加部分处理逻辑。

posted on 2010-12-10 17:29  tax  阅读(829)  评论(0)    收藏  举报