我写了个关于翻页的程序,可是点击翻页的时候第二页的数据是出来了
但是第一页的数据也附带一起显示了出来,请问这是什么原因

解决方案 »

  1.   

    主要因为代码有点长,既然如此我就贴出来吧
    public void browse()
    {
    DBConn conn=new DBConn();
    DataSet myds=new DataSet();
    myds=conn.ExecuteQuery("select * from newsdocinfo");
    if(mix<=0) //该处判断最小页是否为0或-1
    {
    mix=1;
    }
    if(mix==1)
    {
    this.LinkButton1.Visible=false;
    this.LinkButton2.Visible=false;
    this.LinkButton3.Visible=true;
    this.LinkButton4.Visible=true;
    }
    int max1=0;
    if(myds.Tables[0].Rows.Count%RowNum!=0)      //判断记录总数除以最大页数后是否为整数
    {
    max1=myds.Tables[0].Rows.Count/RowNum+1;   //如果不为整数,该最大页数就加1
    }
    else
    {
    max1=myds.Tables[0].Rows.Count/RowNum;
    }
    if(mix>max1)                //该处判断当前页不要超过最大页数
    {
    mix=max1;
    }
    if(mix<max1 && mix>1)    //该处判断如果当前页不是最大页数或当前页不是最小页数
    {
    this.LinkButton1.Visible=true;
    this.LinkButton2.Visible=true;
    this.LinkButton3.Visible=true;
    this.LinkButton4.Visible=true;
    }
    if(mix==max1)
    {
    this.LinkButton1.Visible=true;
    this.LinkButton2.Visible=true;
    this.LinkButton3.Visible=false;
    this.LinkButton4.Visible=false;
    }
    this.Label2.Text=Label2.Text+"<TABLE style='WIDTH: 606px; HEIGHT: 280px' cellSpacing='0' cellPadding='0'width='606' border='1' borderColor='#ff9900'>";
    for(int i=0;i<myds.Tables[0].Rows.Count;i++)
    {
    if(i>=RowNum*(mix-1) && i<(mix)*RowNum) //该处判断总记录数是否大于或小于该页记录数乘该页的值,该方法可以得到每页所需的记录条数 PS;如果不判断大于或等于的话记录将会少显示一条
    {
    Label2.Text=Label2.Text+"<TR><TD>"+"·"+"<a href=target=_blank>"+myds.Tables[0].Rows[i].ItemArray[0].ToString()+""+myds.Tables[0].Rows[i].ItemArray[1].ToString()+""+myds.Tables[0].Rows[i].ItemArray[2].ToString()+"</a></TD></TR>";
    }
    }
    Label2.Text=Label2.Text+"</TABLE>";
    if(myds.Tables[0].Rows.Count%RowNum!=0) //此处判断同上
    {
    int cnt=0;
    cnt=myds.Tables[0].Rows.Count/RowNum+1;   
    this.Label3.Text=""+mix; //把当前页和总页数赋给标签
    this.Label4.Text=""+cnt;
    }
    else
    {
    this.Label3.Text=""+mix;
    this.Label4.Text=""+myds.Tables[0].Rows.Count/RowNum;
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
    this.LinkButton2.Click += new System.EventHandler(this.LinkButton2_Click);
    this.LinkButton3.Click += new System.EventHandler(this.LinkButton3_Click);
    this.LinkButton4.Click += new System.EventHandler(this.LinkButton4_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void LinkButton1_Click(object sender, System.EventArgs e)
    {
    mix=1; //该事件获取最小页数(第一页的页数)
    browse();
    this.TextBox1.Text=mix.ToString();
    } private void LinkButton2_Click(object sender, System.EventArgs e)
    {
    int nums=0;
    try //抛出异常让该事件始终不为-1
    {
    nums=Int32.Parse(this.TextBox1.Text);
    }
    catch(Exception ee)
    {
    nums=1;
    }
    mix=nums-1;
    browse();
    this.TextBox1.Text=mix.ToString();
    } private void LinkButton3_Click(object sender, System.EventArgs e)
    {
    int nums=0;
    try
    {
    nums=Int32.Parse(this.TextBox1.Text);
    }
    catch(Exception ex)
    {
    nums=1;
    }
    mix=nums+1;
    browse();
    this.TextBox1.Text=mix.ToString();
    } private void LinkButton4_Click(object sender, System.EventArgs e)
    {
    int max2=0;
    DBConn conn=new DBConn();
    DataSet myds2=new DataSet();
    myds2=conn.ExecuteQuery("select * from newsdocinfo");
    if(myds2.Tables[0].Rows.Count%RowNum!=0)
    {
    max2=myds2.Tables[0].Rows.Count/RowNum+1; //这里重新设置变量max的值为最大页数的值,将其赋于变量mix,这样一来mix的值就能等于最大页数了
    }
    else
    {
    max2=myds2.Tables[0].Rows.Count/RowNum;
    }
    mix=max2;
    this.TextBox1.Text=mix.ToString();
    browse();
    myds2.Clear();
    }