用的gridview的allowpaging属性自动分页,有查询功能。在点击页码事件中我重新绑定了查询后的结果集,但是我刷新页面,显示结果仍为查询结果。。
 protected void Page_Load(object sender, EventArgs e)
    {
        //Session["isSearch"] = null;
         bindFreight();//自定义方法
       
    }    protected void bindFreight()
    {
        
        string sql = "select * from cargo";
        gvFreight.DataSource = dataOperate.getDataset(sql, "cargo");   //调用getDataset方法将返回值绑定到GridView上
        
        gvFreight.DataBind();                                               
    }    protected void gvFreight_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        string sql;
        sql = Convert.ToString(Session["searchSql"]);
        if (sql==null)
        {
            gvFreight.PageIndex = e.NewPageIndex;
            gvFreight.DataBind();
        }
        else
        {
            gvFreight.DataSource = dataOperate.getDataset(sql, "cargo");   //调用getDataset方法将返回值绑定到GridView上
            gvFreight.PageIndex = e.NewPageIndex;
            gvFreight.DataBind();
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string table ="cargo";
        string keyType1 = "ID";
        string keyType2 = "terminal";
        string keyType3 = "date";
        string keyType4 = "trainNumber";
        string key1 = orderID.Text;
        string key2 = terminal.Text;
        string key3 = leaveDate.Text;
        string key4 = trainNumber.Text;
        string sql;
        if (key1 != "" || key2 != "" || key3 != "" || key4 != "")
        {
            sql = "select * from " + table + " where " + keyType1 + " like '%" + key1 + "%' and " + keyType2 + " like '%" + key2 + "%' and " + keyType3 + " like '%" + key3 + "%' and " + keyType4 + " like '%" + key4 + "%'";//
            Session["searchSql"] = sql;
            
            
            gvFreight.DataSource = dataOperate.getDataset(sql, "cargo");   //调用getDataset方法将返回值绑定到GridView上
            gvFreight.DataBind();
            //Session["searchType"] = ddlSearchType.SelectedValue.ToString();
            
        }
        else
        {
            sql="";
            RegisterStartupScript("false", "<script>alert('请输入查询条件!')</script>"); 
        }
        
    }
gridview分页

解决方案 »

  1.   

    查询就是查询 分页就是分页  你用Session["searchSql"] 传值过去有什么好处?要我写我不会这么写 ,要么就分开来
      

  2.   

    protected void Page_Load(object sender, EventArgs e)
        {
            if(!ISPostBack)
    {
             bindFreight();//自定义方法
    }
           
        }
      
    *****************************************************************************
    签名档: http://feiyun0112.cnblogs.com/
      

  3.   

    你先加上: 
                if (!IsPostBack)
                {}你这样做的目的是 查询中也有分页?
      

  4.   


    加了这个后,查询前点击第二页会报错,ExecuteReader: CommandText 属性尚未初始化;而查询后刷新页面还是只显示查询结果。。
      

  5.   

    protected void gvFreight_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
    去掉代码
        }
      

  6.   

    去掉代码第一次加载会没有  点查询会出来    ,你查询里面要传值过去的不是SQL代码,你知道为什么你刷新后还是第二页吗?你在点浏览器的刷新后还会记录你前面点击过的查询 ,还会把你那个查询传过去  ,所以你的查询传给分页的应该不是SQL,应该传个判断条件过去,分页里面根据判断条件绑定值,绑定数据你可以写个返回datatable的方法
      

  7.   

    我的网页也是使用数据绑定的分页运行无法翻页。
    你直接写翻页的查询,然后把数据填充GridView。查询的时候使用子查询,来查询固定位置的固定数目sql记录。
    select top n * from Table where id not in (select top m id from Table)
      

  8.   

    这样的话,如果记录特别多的话,那第三页、第四页也会麻烦啊要是说语句的话,n为每页显示的条目数,m为n*(现在页数-1),弄一个通用的公式即可。但是这种方法的麻烦在于页数越靠后,数据量越大,搜索的速度越慢。不过,对于小数据是可以的。