我的问题是在页面上有5个控件:
一个TextBox1,
一个Button1,
一个DataList1,
两个HyperLink(上一页,下一页)
我想在文本框中输入一个搜索关键字,点按钮把结果查询到DataList1中,
另外可以翻页.Button1_Click事件如下:
protected void Button1_Click(object sender, EventArgs e)
{
    string keyword = TextBox1.Text;
    string sql="select * from t1 where aa='"+keyword+"'";
    band(keyword); //绑定并包含有翻页                    
}band()方法如下:
private void bandd(string KeyWord)
{
    //省略部分代码
    if (!pages.IsFirstPage)
    {
        hpl_prev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(indexpage - 1);  //上一页
    }    if (!pages.IsLastPage)
    {
        hpl_next.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(indexpage + 1);  //下一页
    }    //省略下面代码}运行时查询结果出来了,但点击下一页时就没有数据了!请问怎么解决?分不够再加

解决方案 »

  1.   

    点击翻页事件中,还要绑定DataList1才行
      

  2.   

    HyperLink 的代码贴出来看看,绑定datalist没
      

  3.   

    datalist翻页/// <summary>
      /// DataList翻页程序
      /// </summary>
      private void DisplayBySection()
      {
       this.dlDoctor.Dispose();
       string SectionID = Request.Params.Get("SectionID");
       string strSectionID;
       if(SectionID!=null)
       {
        strSectionID = "select * from Doctors,Sections,BigSections,DoctorType "+
         "where Sections.BigSectionID = BigSections.BigSectionID and Sections.SectionID = Doctors.SectionID "+
         "and Doctors.DoctorTypeID = DoctorType.DoctorTypeID and DoctorType.DoctorTypeName='祖传医师'"+
         "and Doctors.SectionID = '"+SectionID+"'";
       }
       else
       { 
        strSectionID = "select * from Doctors,Sections,BigSections,DoctorType "+
          "where Doctors.SectionID = Sections.SectionID and BigSections.BigSectionID = Sections.BigSectionID and "+
          "Doctors.DoctorTypeID=DoctorType.DoctorTypeID and DoctorType.DoctorTypeName='祖传医师' order by DoctorID";
       }
       this.dlDoctor.Dispose();
       try
       {
        DataSet dsDoctor = PubClass.DbOperate.ExecuteSqlDataAdapter(strSectionID);    //使用页面内置的数据源PagedDataSource(具有翻页功能)
        PagedDataSource objPage = new PagedDataSource();
        objPage.DataSource = dsDoctor.Tables[0].DefaultView;
        objPage.AllowPaging =true;
        objPage.PageSize = 9;
        int curPage;
        if(Request.QueryString["Page"]!=null)
        {
         curPage = Int32.Parse(Request.QueryString["Page"].ToString()) ;
        }
        else
        {
         curPage = 1;
        }
        objPage.CurrentPageIndex = curPage -1;   //lblCurPage为显示当前页的Lable控件
        lblCurPage.Text = "当前页: 第"+curPage.ToString()+"页";
        if(!objPage.IsFirstPage)
        {
         this.lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(curPage-1);
        }
        if(!objPage.IsLastPage)
        {
         this.lnkNext.NavigateUrl = Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(curPage+1);
        }
        
        this.dlDoctor.DataSource = objPage;
        this.dlDoctor.DataBind();   
       }  
       catch(Exception Err)
       {
        Response.Write(PubClass.CommonTool.PopShow(Err.Message));
       }
      }
      

  4.   


    DataList翻页2008-01-09 14:27protected System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    protected System.Data.SqlClient.SqlCommand sqlCommand1 = new System.Data.SqlClient.SqlCommand();
    protected System.Data.SqlClient.SqlDataReader sqlDataReader1;
    protected System.Data.DataSet dataSet1;
    protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;private void Page_Load(object sender, System.EventArgs e)
    {sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter("select * from db","Data Source=.; Initial Catalog=db;User id=sa; pwd=sa");
         dataSet1 = new DataSet();
         sqlDataAdapter1.Fill(dataSet1);
      
         PagedDataSource pds = new PagedDataSource();
         pds.DataSource = dataSet1.Tables[0].DefaultView;
         pds.AllowPaging = true;
         pds.PageSize = 10;     int CurPage1=1;
         
         pds.CurrentPageIndex = CurPage1 - 1;
         DIV1.InnerHtml = "当前页:第 " + CurPage1.ToString() + " 页 ,共 " + pds.PageCount + " 页";
      
         if (!pds.IsFirstPage)
         {
          Prv.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage1 - 1);
         
          Top.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
         }
         if (!pds.IsLastPage)
         {
          Dow.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage1 + 1);
         
          Bot.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + pds.PageCount;
         }
             DataList1.DataSource = pds;
         DataList1.DataBind();}
     
      

  5.   

    刚才没注意看,楼主下一页写成了URL形式,直接重新加载页面了,相当于本页重新刷新
    hpl_next.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(indexpage + 1);  贴出你page_load的代码看看
      

  6.   

    这个问题是由于ViewState引起的:当Button1按下的时候,你的代码调用Band()方法绑定了数据,这个过程关键字是保存在ViewState里的,但是你通过HyperLink产生的链接定位到下一页的时候,相当于重新载入了页面进入初始状态(也就是IsPostedBack==false),所以先前的状态就丢失了,回到了没有进行任何搜索的状态,也就不会显示任何东西了。解决的方案有两种:1)把Keyword一起在URL中传出:Button1_Click事件如下: 
    protected void Button1_Click(object sender, EventArgs e) 

        string keyword = TextBox1.Text; 
        string sql="select * from t1 where aa='"+keyword+"'"; 
        band(keyword); //绑定并包含有翻页                    
    } band()方法如下: 
    private void bandd(string KeyWord) 

        //省略部分代码 
        if (!pages.IsFirstPage) 
        { 
            // hpl_prev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(indexpage - 1);  //上一页 
             hpl_prev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(indexpage - 1) + "&kw="+keyword;  //上一页     }     if (!pages.IsLastPage) 
        { 
            // hpl_next.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(indexpage + 1);  //下一页 
             hpl_next.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(indexpage + 1) + "&kw="+keyword;  //下一页     }     //省略下面代码 } void override OnLoad(EventArgs e){
        // 这里需要添加更多的验证逻辑...
        if(Request.QueryString["kw"] != null){
            Band(Request.QueryString["kw"];
        }
    }
    2)用LinkButton代替HyperLink,把页码存到CommandArgument中,实现代码片段为:protected LinkButton lb_prev;
    protected LinkButton lb_next;// ...
    lb_prev.Click += new EventHandler(lb_prev_Click);
    lb_next.Click += new EventHandler(lb_next_Click);
    // ...void lb_prev_Click(object sender, EventArgs e){
        int curPage = Convert.ToInt32(lb_prev.CommandArgument);
        // ...
    }void lb_next_Click(object sender, EventArgs e){
        // ...
    }private void bandd(string KeyWord) 
    {     //省略部分代码 
        if (!pages.IsFirstPage) 
        { 
            lb_prev.CommandArgument = 1;
        }     if (!pages.IsLastPage) 
        { 
            lb_next.CommandArgument = PageCount;
        }     //省略下面代码 } 
      

  7.   

    this.dlDoctor.Dispose();
    这个是什么意思的?
      

  8.   

    点击查询按钮后还要再给DataList绑定一下数据
      

  9.   

    1.把你Page_load中绑定的数据放在if(!Page.IsPostBack)中2.点击查询按钮后把你的数据再重新绑定一下.(重调一下你绑定数据的方法)