网页里用datagrid1将1个数据表分页显示出来,同时网页里还放有一个dropdownlist(ID=dropCat),供用户选择数据表中“类型”字段的值,再用查询按钮查询(ID=btnSearch)。网页初始加载时显示数据表里所有内容,需要显示某一类型的内容,在dropCat里选好类型后点击查询按钮,datagrid1就将该类型的信息显示出来。
现在问题是这样,第一次进到这个页面,也就是在第一页时,选好类型查询没有一点问题,但是如果我先翻页,比如到第二页,再选类型查询就会出错,提示“Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount”。我在网页里用了label来显示CurrentPageIndex和PageCount的。
所以我就不明白了,难道翻了页后的dataview(datagrid1的数据源是dataview)就变了?

解决方案 »

  1.   

    源码也放上来吧<script runat=server>
    dim dv as dataview
    dim dt as datatable
    sub page_load(sender as object, e as eventargs)   
        dim provider,SQL,ConnStr as string    
        provider="microsoft.jet.oledb.4.0;"
        ConnStr = "Provider="+Provider+"Data Source="+server.mappath("/db/Archives.mdb") +";Persist Security Info=False;User ID=;Jet OLEDB:Database password=123"
        SQL="select * from Archives"
        dim cmd as oledbdataadapter
        cmd=new oledbdataadapter(SQL,ConnStr)
        dim ds as dataset=new dataset()
        cmd.fill(ds,"Archives")
        dt=ds.tables("Archives")   
        dv=new dataview(dt)    
        DataGrid1.DataSource = dv        
        DataGrid1.DataBind  
      else
         response.redirect("warning4.aspx")
      end if
    end sub
    Sub DataGrid1_Page(sender As Object, e As DataGridPageChangedEventArgs)
            DataGrid1.CurrentPageIndex = e.NewPageIndex
            BindGrid
    End Sub
    sub btnSearch_Click(sender as object,e as eventargs)
       
        dim provider,SQL,ConnStr as string
        provider="microsoft.jet.oledb.4.0;"
        ConnStr = "Provider="+Provider+"Data Source="+server.mappath("/db/Archives.mdb") +";Persist Security Info=False;User ID=;Jet OLEDB:Database password=123"
        if dropCat.SelectedIndex<>0 then
           SQL="select * from Archives where Archives_Cat='"& dropCat.SelectedItem.text &""    
        else
           SQL="select * from Archives"
        end if
        dim cmd as oledbdataadapter
        cmd=new oledbdataadapter(SQL,ConnStr)
        dim ds as dataset=new dataset()
        cmd.fill(ds,"Archives")
        dt=ds.tables("Archives")
        dv=new dataview(dt)    
        DataGrid1.DataSource = dv        
        DataGrid1.DataBind    
        end sub
    *****************************************************
    希望各位能有耐心看我的帖子,鞠躬!
      

  2.   

    绑定的时候放到
    if( !this.IsPostBack)
    {
    dg.DataBind();//在这里进行绑定
    }
      

  3.   

    再次点击查询时先把datagrid的页设置为第一页DataGrid1.CurrentPageIndex = 0
      

  4.   

    http://goody9807.611.cn/Announce/Announce.asp?BoardID=2&ID=546
      

  5.   

    因为,你翻页后,DataGride.CurrentPageIndex在回调时并没有回到0,或者说可能已经超出你选择另一种类型查询所得到的总页数.所以,你最好在分类查询前将DataGrid.CurrentPageIndex = 0
      

  6.   

    因为,你翻页后,DataGride.CurrentPageIndex可能已经超出你选择另一种类型查询所得到的总页数.
    所以,你最好在分类查询前将DataGrid.CurrentPageIndex = 0
      

  7.   

    在查询按扭事件中重新设置DataGride.CurrentPageIndex
      

  8.   

    用AspNetGrid控件作分页,
    #region Page_Seperate
    public void BindData(string Sql,string Sql1,DataGrid DataGrid1,Wuqi.Webdiyer.AspNetPager AspNetPager1,string DsName)
    {
    try
    {
    ConnDB();
    MyConn.Open();
    SqlCommand MyComm = new SqlCommand(Sql, MyConn);
    AspNetPager1.RecordCount = (int)MyComm.ExecuteScalar();
    SqlCommand MyComm1 = new SqlCommand(Sql1, MyConn);
    SqlDataAdapter Adapter = new SqlDataAdapter(MyComm1);
    DataSet Ds = new DataSet();
    Adapter.Fill(Ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize,DsName);
    DataGrid1.DataSource = Ds.Tables[0].DefaultView;
    DataGrid1.DataBind();
    }
    catch(Exception ex)
    {
    throw new Exception(ex.Source +ex.Message +ex.StackTrace); 
    }
    }
    #endregion
      

  9.   

    这个问题的原因是你选择类型之后,所得到的记录的页数比你当前在的页数小(因为在你选择类型之后当前页不会自动回到第一页),所以你出现这个错误。
    所以在你选择类型之后,首先把DataGrid.CurrentPageIndex = 0。
    但是这样又会出现另一个问题(如果你的DataGrid.CurrentPageIndex = 0在绑定代码之中),
    你点翻页的时候永远只会在第一页,因为CurrentPageIndex,始终等于0,
    所以选择类型之后绑定和翻页绑定因为有不同的方法,而代码就是一个存在DataGrid.CurrentPageIndex = 0(选择类型绑定),另一个则不存在(翻页绑定)