当前位置:编程学习 > C#/ASP.NET >>

gridview分页

求助,关于动态绑定gridview数据,如何实现分页。谢谢大家  --------------------编程问答-------------------- 三种方法:(1):gridview自带的分页
          (2):控件,比如AspNetPager
         (3):自己写代码 --------------------编程问答-------------------- 去 百度 搜索一大堆····

csdn 搜索也有一大堆··· --------------------编程问答-------------------- 小问题自己解决啦··· --------------------编程问答--------------------
如果数据量多的话,用AspNetPager 控件
--------------------编程问答-------------------- 最好使用AspNetPager控件,gridview自带的那个不好用。
--------------------编程问答--------------------
引用 1 楼 chinaway1984 的回复:
三种方法:(1):gridview自带的分页 
          (2):控件,比如AspNetPager 
        (3):自己写代码




 完全正确!!!!!1 --------------------编程问答-------------------- 自己写吧,我就是自己写用存储过程分页 --------------------编程问答-------------------- 写 sql 来分  速度快 
数据不对就 用自带的 --------------------编程问答-------------------- sql 存储过程分页
ASPNETpager分页控件分有
gridview自定义分页 --------------------编程问答--------------------
引用 1 楼 chinaway1984 的回复:
三种方法:(1):gridview自带的分页 
          (2):控件,比如AspNetPager 
        (3):自己写代码


推荐使用第二种,第一种样式太丑,第三种麻烦。 --------------------编程问答-------------------- 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" datasourceid="SqlDataSource1"
            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> 
    
    
        <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
            providername="System.Data.SqlClient" selectcommand="SELECT [CompanyName], [ContactTitle], [Phone], [Fax], [ContactName] FROM [Customers]">
        </asp:sqldatasource>
PageIndexChanging处理程序:
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView theGrid = sender as GridView;  // refer to the GridView
        int newPageIndex = 0;
        if (-2 == e.NewPageIndex) { // when click the "GO" Button
            TextBox txtNewPageIndex = null;
            //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
//updated at 2006年6月21日3:15:33
            if (null != pagerRow) {
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
            }
            if (null != txtNewPageIndex) {
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
            }
        }
        else {  // when click the first, last, previous and next Button
            newPageIndex = e.NewPageIndex;
        }
        // check to prevent form the NewPageIndex out of the range
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
        // specify the NewPageIndex
        theGrid.PageIndex = newPageIndex;
        // rebind the control
        // in this case of retrieving the data using the xxxDataSoucr control,
        // just do nothing, because the asp.net engine binds the data automatically
    }
注意到,上面的示例中,由于增加了一个跳转按钮GO,但是asp。net不支持相关的CommandArgument值,虽然可以将Go Button的Commandname设为Page,还需要手动的在PageIndexChanging增加部分处理逻辑。  --------------------编程问答-------------------- 很基础的东西,自己多研究下嘛~ --------------------编程问答--------------------  传统的方法用这个函数应该没有问题onpageindexchanging="GridView1_PageIndexChanging,
protected void GridView_MaintainerShip_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //设置显示的页数;
        this.GridView_MaintainerShip.PageIndex = e.NewPageIndex;
       this.DataBind();
        //重新绑定GridView
           }
但是在我的gridview 控件中只要写上Allowpaging="true"就显示不出数据 --------------------编程问答--------------------
引用 1 楼 chinaway1984 的回复:
三种方法:(1):gridview自带的分页 
          (2):控件,比如AspNetPager 
        (3):自己写代码

正解.
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,