DataList分页功能

上次为了DataList分页的分页功能,花的不少时间,网上查的不了资料,都没有得到好的结果.根据资料最终实现的二种分页方法,一是这了以后作参考,另外给需要的人作借鉴

//第一种方法:
public void dlBind(string moreFlag1)
    {

        //定义数据库的Connection and Command
        int curPage = Convert.ToInt32(this.label_showCurPage.Text);

        dataDeal dataConn = new dataDeal();
        if (dataConn.sqlConn.State == System.Data.ConnectionState.Closed)
        {
            dataConn.sqlConn.Open();
        }
        SqlCommand myCommand = new SqlCommand("GetTypeNews", dataConn.sqlConn);
        myCommand.Parameters.Add("newsTypeNo", SqlDbType.VarChar, 2);
        myCommand.Parameters["newsTypeNo"].Value = moreFlag1;
        myCommand.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter MainAdaptor = new SqlDataAdapter(myCommand);

        MainAdaptor.Fill(ds, "newsTable");

        System.Web.UI.WebControls.PagedDataSource turnPage = new PagedDataSource();
        turnPage.DataSource = ds.Tables["newsTable"].DefaultView;
        turnPage.AllowPaging = true;
        turnPage.PageSize = 10;
        turnPage.CurrentPageIndex = curPage - 1;
        this.label_showCurPage.Text = Convert.ToString(curPage);
        this.btn_upPage.Enabled = true;
        this.btn_nextPage.Enabled = true;
        this.btn_firstPage.Enabled = true;
        this.btn_lastPage.Enabled = true;
        if (curPage == turnPage.PageCount)
        {
            this.btn_lastPage.Enabled = false;//下一页显示控制为无效
            this.btn_nextPage.Enabled = false;//最后一页控制为无效
        }
        if (curPage == 1)
        {
            this.btn_firstPage.Enabled = false;//下一页显示控制为无效
            this.btn_upPage.Enabled = false;//最后一页控制为无效
        }
        this.label_pageCount.Text = Convert.ToString(turnPage.PageCount);
        ps.DataSource = turnPage;
        ps.DataBind();
        dataConn.sqlConn.Close();// Close();
    }

 

  protected void btn_firstPage_Click(object sender, EventArgs e)
        {
            this.label_showCurPage.Text = "1";
            this.dlBind(this.label_type.Text.Trim());
        }
        protected void btn_upPage_Click(object sender, EventArgs e)
        {
            this.label_showCurPage.Text = Convert.ToString(Convert.ToInt32(this.label_showCurPage.Text) - 1);
            this.dlBind(this.label_type.Text.Trim());
        }
        protected void btn_nextPage_Click(object sender, EventArgs e)
        {
            this.label_showCurPage.Text = Convert.ToString(Convert.ToInt32(this.label_showCurPage.Text) + 1);
            this.dlBind(this.label_type.Text.Trim());
        }
        protected void btn_lastPage_Click(object sender, EventArgs e)
        {
            this.label_showCurPage.Text = this.label_pageCount.Text;
            this.dlBind(this.label_type.Text.Trim());
        }


//第二种方法:


public partial class Default2 : System.Web.UI.Page
{
    SqlDataBase link = new SqlDataBase();
    private static string cmdstr = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["item"] != null)
        {
            this.Lb_title.Text= Request.QueryString["item"];
            cmdstr="select FileNo ,ItemType,(case when len(filetitle)>18 then substring(filetitle,0,18)+'... ' else filetitle  end ) as filetitle,filetitle as filetitle1, content ,username ,hitnum, sendtime from files_tb where itemtype='"+this.Lb_title.Text.Trim()+"'" ;

            // MyDataBinder();
          //  DataBind();
       
        }
        if (!Page.IsPostBack)
        {
            this.pagesize.Text = "2";
            this.currentpage.Text = "0";
            DataBind();
        }
       
      

       
        if (Request.QueryString["fileno"] != null)
        {

            //Int32 getfileid = Int32.Parse(Request.QueryString["fileno"]);
            //DataSet ds = link.GetDs("select * from files_tb where fileno='" + getfileid + "'", link.DBconn());
            //this.Label1.Text = ds.Tables[0].Rows[0][2].ToString();
            //this.TextArea1.Value = ds.Tables[0].Rows[0][3].ToString();
            //this.Label2.Text = ds.Tables[0].Rows[0][6].ToString();
            //this.Label3.Text = "发布者:" + ds.Tables[0].Rows[0][4].ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      // this.Label1.Text=Request.QueryString["fileno"];
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {


       // this.Label1.Text = Request.QueryString["fileno"];
      
       
    }

    private void DataBind()
    {
        if (Request.QueryString["item"] != null)
        {

            SqlDataAdapter objDA = new SqlDataAdapter(cmdstr, link.DBconn());
            DataSet objDS = new DataSet();

            if (!Page.IsPostBack)
            {
                objDA.Fill(objDS, "mytable1");
                this.totalcount.Text = objDS.Tables[0].Rows.Count.ToString();
                objDS = null;
                objDS = new DataSet();
            }


            objDA.Fill(objDS, Int32.Parse(this.currentpage.Text), Int32.Parse(this.pagesize.Text), "mytable2");

            this.DetailList.DataSource = objDS.Tables[0].DefaultView;
            DetailList.DataBind();
            link.DBconn().Close();
            PrintStatus();
        }
    }

   
     private void PrintStatus()
       {
          this.lbstatus.Text   =   "总记录数:<b>"   +   this.totalcount.Text  ;
          lbstatus.Text += "</b>   当前:<b>   ";

          lbstatus.Text += Convert.ToString((Int32)(Int32.Parse(this.currentpage.Text) / Int32.Parse(this.pagesize.Text) + 1));
          lbstatus.Text += "</b>/<b>";

          if ((Int32.Parse(totalcount.Text) % Int32.Parse(pagesize.Text)) > 0)
          {
              lbstatus.Text += Convert.ToString((Int32)(Int32.Parse(totalcount.Text) / Int32.Parse(pagesize.Text) + 1));
          }
          else
          {
              lbstatus.Text += Convert.ToString(Int32.Parse(this.totalcount.Text) / Int32.Parse(pagesize.Text));
          }
          lbstatus.Text += "</b>";
        }

 

 

    protected void firstpage_Click(object sender, EventArgs e)
    {
        this.currentpage.Text = "0";
        DataBind();
    }
    protected void lastpage_Click(object sender, EventArgs e)
    {
       Int32 tmpInt ;

        tmpInt = Int32.Parse(this.totalcount.Text) % Int32.Parse(this.pagesize.Text);
        if (tmpInt > 0)
        {
            this.currentpage.Text =Convert.ToString(Int32.Parse(totalcount.Text) - tmpInt);
        }
        else
        {
            currentpage.Text = Convert.ToString(Int32.Parse(totalcount.Text) - Int32.Parse(pagesize.Text));
        }
        DataBind();
    }
    protected void preivepage_Click(object sender, EventArgs e)
    {
            
        this.currentpage.Text = Convert.ToString(Int32.Parse(this.currentpage.Text) - Int32.Parse(pagesize.Text));
              if (  Int32.Parse(this.currentpage.Text)< 1 )
              {  
              this.currentpage.Text   =   "0"   ;
              this.preivepage.Enabled = false;
            
              } 
            this.nextpage.Enabled = true;
              DataBind();
    }
    protected void nextpage_Click(object sender, EventArgs e)
    {
        this.preivepage.Enabled = true;
        if (Int32.Parse(this.currentpage.Text)+1 < Int32.Parse(this.totalcount.Text))
        {
            currentpage.Text = Convert.ToString(Int32.Parse(this.currentpage.Text) + Int32.Parse(pagesize.Text));
        }
        if (Int32.Parse(this.currentpage.Text) + Int32.Parse(pagesize.Text) >= Int32.Parse(this.totalcount.Text))
        {
           
            this.nextpage.Enabled = false;
        }
        DataBind();
    }
    protected void turntopage_Click(object sender, EventArgs e)
    {
         Int32 pagemax=0;
        if ((Int32.Parse(totalcount.Text) % Int32.Parse(pagesize.Text)) > 0)
        {
            pagemax= (Int32)(Int32.Parse(totalcount.Text) / Int32.Parse(pagesize.Text) + 1);
        }
        else
        {
           pagemax=(Int32)(Int32.Parse(this.totalcount.Text) / Int32.Parse(pagesize.Text));
        }


        if (Int32.Parse(this.TextBox1.Text) > pagemax)
        {
            Response.Write("<script>alert(/"己超过最大页数!/")</script>");
        }
          
        this.currentpage.Text = this.TextBox1.Text;
        currentpage.Text = Convert.ToString(Int32.Parse(this.TextBox1.Text) *Int32.Parse(pagesize.Text)-2);
    
        DataBind();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值