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

使用GridView控件,利用其PageIndexChanging事件实现分页却不能实现,请问该如何解决,谢谢!

html页面的代码如下:
<asp:GridView CssClass ="top" ID="gv_class_list" runat="server" AutoGenerateColumns ="false" DataKeyNames ="classID" CellPadding ="4"
         GridLines ="none" Width ="100%" OnPageIndexChanging="gv_class_list_PageIndexChanging" PageIndex ="15" AllowPaging ="true" >
            <Columns >
                <asp:BoundField DataField ="grade" HeaderText ="年级" />
                <asp:HyperLinkField DataNavigateUrlFields="classID" DataNavigateUrlFormatString="" HeaderText ="专业(专业方向)" DataTextField="speciality" />
                <asp:BoundField DataField ="numbers" HeaderText ="人数" />
                <asp:BoundField DataField ="headTeacher" HeaderText ="班主任" />
            </Columns>
            <PagerTemplate>
                <table style="font-size :12px;height:50px;">
               <tr>            
                 <td> 总共<asp:Label ID="Label1" runat="server" Text="<%#((GridView)Container.NamingContainer).PageCount %>"></asp:Label>页   </td>
                 <td> 第<asp:Label ID="Label2" runat="server" Text="<%#((GridView)Container.NamingContainer).PageIndex+1 %>"></asp:Label>页  </td>
                 <td> <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument ="1" CommandName ="Page" Enabled ="<%#((GridView)Container.NamingContainer).PageIndex!=0 %>">首页</asp:LinkButton>  </td>
                 <td><asp:LinkButton ID="LinkButton2" runat="server" CommandArgument ="Prev" CommandName ="Page" Enabled ="<%#((GridView)Container.NamingContainer).PageIndex!=0 %>">上一页</asp:LinkButton>  </td>
                 <td><asp:LinkButton ID="LinkButton3" runat="server" CommandArgument ="Next" CommandName ="Page" Enabled ="<%#((GridView)Container.NamingContainer).PageIndex!=((GridView)Container.NamingContainer).PageCount-1 %>">下一页 </asp:LinkButton>  </td>
                 <td><asp:LinkButton ID="LinkButton4" runat="server" CommandArgument ="Last" CommandName ="Page" Enabled ="<%#((GridView)Container.NamingContainer).PageIndex!=((GridView)Container.NamingContainer).PageCount-1 %>">尾页</asp:LinkButton>  </td>
                 <td><asp:LinkButton ID="LinkButton5" runat="server" CommandArgument ="-1" CommandName ="Page" ValidationGroup="1" >GO</asp:LinkButton> </td>
                 <td><asp:TextBox ID="txtNum" runat="server" Width="30px" Text ="<%#((GridView)Container.NamingContainer).PageIndex+1 %>" ValidationGroup="1"></asp:TextBox>
                 <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="" ValidationExpression ="^\d+$" ControlToValidate ="txtNum" ValidationGroup="1"></asp:RegularExpressionValidator></td>
               </tr>
               </table>

            </PagerTemplate>
            <RowStyle HorizontalAlign="center"  />           
        </asp:GridView>

后台代码如下:
protected void bind()
    {        
        DataSet ds = null;       
        
        try
        {
            ds=DataBusiness.DataExecute.ExecuteDataset(DataBusiness.DataExecute.CONN_STRING, CommandType.StoredProcedure, "getAllClass");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        
        if (ds.Tables[0].Rows.Count != 0)
        {
            this.gv_class_list .DataSource = ds;
            this.gv_class_list .DataBind();
            //this.Count = ds.Tables[0].Rows.Count;            
        }        
    }

    protected void gv_class_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gv = (GridView)sender;
        int newPageIndex = 0;
        if (2 == e.NewPageIndex)
        {
            TextBox tb = null;
            GridViewRow gvr = gv.BottomPagerRow;
            if (gvr != null)
            {
                tb = (TextBox)gvr.FindControl("txtNum");

            }
            if (tb != null)
            {
                newPageIndex = int.Parse(tb.Text) - 1;
            }
        }
        else
        {
            newPageIndex = e.NewPageIndex;
        }
        newPageIndex = newPageIndex <= 0 ? 0 : newPageIndex;
        this.gv_class_list.PageIndex = newPageIndex;
        //分页后重新绑定数据源
        this.bind();  
} --------------------编程问答-------------------- 怎么没人的?顶一下! --------------------编程问答-------------------- 设定PageSize=?,AutoPaging=True --------------------编程问答-------------------- gridview有自带的分页,用那个不用写事件就能实现,如果要自己手写代码实现分页的话,用pagedatasource 在bind()里判断页数什么的... --------------------编程问答-------------------- 就在控件的属性里设置一下 --------------------编程问答-------------------- up --------------------编程问答--------------------   protected void gv_class_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv_class_list.PageIndex = e.NewPageIndex;
      //重新绑定
    } --------------------编程问答-------------------- 看我这个:
自定义GridView分页模板 
http://www.cnblogs.com/Jinglecat/archive/2008/11/08/424794.html --------------------编程问答-------------------- <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>
还是问题,这段代码没显示出来!没效果!
各位大虾帮帮忙 --------------------编程问答-------------------- 帮顶!! --------------------编程问答-------------------- 6楼正解 --------------------编程问答-------------------- --------------------编程问答-------------------- 还没结贴?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,