<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>    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;
        //theGrid.PageIndex = e.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        theGrid.DataSource = DB.ODateSet;
        theGrid.DataBind();
        DB.free();
    }这是网上一个分页例子,单步测试了,点击下一步都无法触发GridView1_PageIndexChanging事件,gridview是空白一片,各位帮帮忙吧,谢谢了!!

解决方案 »

  1.   

    那你是否手动设置了这个GridView的分页属性比如:
    AllowPaging = true;
    等等
      

  2.   

    AllowPaging = true; 设置好了,没用commandargument="Prev"  这样设置好这个参数GridView1_PageIndexChanging事件是会自动识别的吧??
    还有什么可能导致 GridView1_PageIndexChanging事件无法触发呢??
      

  3.   

    自动识别吗?我自己很少用GridView自带的那个分页都是使用自己的分页,所以不是很熟悉这种方式的分页你试过PageIndexChanged这个事件吗?不好意思,这方面的知识我需要再学习学习可能帮不到什么忙
      

  4.   

    你试着在事件中手都判断一下这个CommandArgument然后进行操作试试如果你确认GridView可以自动识别Prev这个CommandArgument的话那就算了
      

  5.   

    说得不清楚,可能误解,我的意思是说项这样配置
     <asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />    
    点击下一页,那个GridView1_PageIndexChanging事件无法激发
      

  6.   

          <asp:gridview id="CustomersGridView" 
            datasourceid="CustomersSource" 
            autogeneratecolumns="true"
            emptydatatext="No data available." 
            allowpaging="true"
            autogenerateeditbutton="true"
            datakeynames="CustomerID"  
            onpageindexchanging="CustomersGridView_PageIndexChanging"
            onrowcancelingedit="CustomersGridView_RowCancelingEdit" 
            runat="server">
      

  7.   

    你意思是,这个下一页,上一页什么的
    都是自己拖上去的button?如果是这样的话应该不行吧因为如果使用了GridView自己的分页的话它便自动会有下一页,上一页的按钮在属性菜单里,PagerSettings设置
      

  8.   

    <asp:GridView ID="GridView1" runat="server" CellPadding="3" 
        HorizontalAlign="Center" Width="980px" AutoGenerateColumns="False" 
        OnPageIndexChanging="GridView1_PageIndexChanging" Font-Size="Smaller" 
        PageSize="5" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" 
        BorderWidth="1px" CssClass="tb_Inner" OnRowDataBound="GridView1_RowDataBound" 
        EmptyDataText="<center>没有搜索到任何记录,请重置搜索条件。</center>" ForeColor="Red"
        EnableViewState="False" AllowPaging="True">这个我写了..,gridview一样是空白一片
      

  9.   

    PagerSettings 我没用PageSetting,不是拖上去的,我是直接把分页模板的代码复制上去,AllowPaging = true;是因为网上的那个例子就是把他设为true,测试发现分页模板的内容把自动生成的分页内容给隐藏了,把模板内容删除后,自动分页产生的分页内容会还原,我想应该不是这个问题吧
      

  10.   

    给GridView控件添加一个事件PageIndexChanging,并设置GridView的属性:AllowPaging="True" protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 

          GridView1.PageIndex = e.NewPageIndex; 
          GridView1.datasource="数据源";       
          GridView1.DataBind(); 

      

  11.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    DataGridDataBind();
    }
    }
     
    //绑定数据
    private void DataGridDataBind()
    {
    DataSet ds = GetCustomersData();
    recordCount = ds.Tables[0].Rows.Count;
    //获取当前的页数
    pageCount = (int)Math.Ceiling( recordCount * 1.0 / PageSize);
    //避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错
    if(recordCount ==0)
    {
    this.DataGrid1.CurrentPageIndex = 0;
    }
    else if(this.DataGrid1.CurrentPageIndex >= pageCount)
    {
    this.DataGrid1.CurrentPageIndex = pageCount - 1;
    }
    this.DataGrid1.DataSource = ds;
    this.DataGrid1.DataBind();
    NavigationStateChange();
    }
      

  12.   

    EnableViewState="False" 改为true,闪分
      

  13.   

    你将分页代码写到Row_Cammnd函数里面吧。
      

  14.   

    给GridView控件添加一个事件PageIndexChanging,并设置GridView的属性:AllowPaging="True" protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 

          GridView1.PageIndex = e.NewPageIndex; 
          GridView1.datasource="数据源";      
          GridView1.DataBind(); 

    这个方法行.