Asp.Net北大青鸟小结(四)-使用GridView实现真假分页

Asp.Net北大青鸟总结(四)-使用GridView实现真假分页

    这段时间看完了asp.net视频,但是感觉到自己的学习好像没有巩固好,于是又在图书馆里借了几本关于asp.net的书感觉真的很好自己大概对于asp.net能够实现基本的小Demo,但是我知道只有真正的使用才能够有所收获,而且自己的认识度还是要进一步的学习,在这一部分的学习中自己也算是对于分页有了一个基本的了解了吧,也用它做出来的几个基本的Demo,那么接下来我们来看一下这个控件的用于真假分页的一些使用方法。

   一.什么是真假分页

    1.假分页:

    假分页虽然在界面上实现了分页的,但是他并没有实现分页,每一次点击页数的时候都要从数据库中讲所有的数据再重新查一遍,也就是说这样每次都从数据库中查询一次那么就会影响他查询速度,所以假分页虽然是实现了界面上的分页但是还是没有实现真正的分页。

   2.真分页:

    那么我们知道如果想要真正的实现分页,就是每次只是查询出想要页面的数据,这样就可以减少每次查询的数据的量而且还可以提高查询的效率,真正的实现了翻一页查一页。

二.基本实现

   1.假分页

   我们视频里的视频比较历史悠久所以当我们学到这里的时候控件已经改了名字了,但是还是换汤不换药,还是要经过这么几个步骤才可以,1>要在gridview中改变他的AllowPaging属性改为true,然后将PageSize设为你想分页的数目,然后接下来就是要将控件绑定数据库

protected void Page_Load(object sender, EventArgs e)  
        {  
            //页面第一次加载  
            if (!Page.IsPostBack )  
            {  
                //绑定数据  
                GridView1.DataSource = MyData();  
                GridView1.DataBind();  
            }  
        }  

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
        {  
            GridView1.PageIndex = e.NewPageIndex;  
            DataTable dt = MyData();  
          
            GridView1.DataSource = dt;  
            GridView1.DataBind();  
  
        }  
      
        private static DataTable  MyData()  
        {  
            SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;password=gss123");  
            con.Open();  
            string strCmd = "select * from comment";  
            SqlCommand cmd = new SqlCommand(strCmd,con );  
            SqlDataReader sdr = cmd.ExecuteReader();  
            DataTable dt=new DataTable ();  
            dt.Load(sdr );  
            con.Close();  
            return dt;  
  
  
        }  
    }  

以上就是假分页的数据绑定。

2.真分页

  真分页可是要花一点功夫的,真分页是需要aspnetpaper这个第三方控件的,需要下载他的DLL文件然后在拖动至工具箱的然后在使用它,他就是一个翻页的工具

 protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!Page.IsPostBack )  
            {  
                //窗体打卡时,起始数据编号为0,终止数据编号为每页的信息条数  
                int intStartIndex = ANP.PageSize * 0;  
                int intEndIndex = ANP.PageSize * 1;  
              <span style="color:#ff0000;"><strong>  ANP.RecordCount = MyAllData().Rows.Count;</strong></span>  
                //绑定数据  
                GridView1.DataSource = MyPartData(intStartIndex, intEndIndex);  
                GridView1.DataBind();  
            }  
        }  
        /// <summary>  
        /// 改变了页数的数据显示  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        protected void ANP_PageChanged(object sender, EventArgs e)  
        {  
            //起始数据编号=每页的信息容量*(当前页数-1)+1;  
            //例:第六页,每页显示5条,第六页的起始数据=5*5+1=26;  
            int intStartIndex=ANP.PageSize * (ANP.CurrentPageIndex-1)+1;  
            int intEndIndex = ANP.PageSize * ANP.CurrentPageIndex;  
            GridView1.DataSource = MyPartData(intStartIndex,intEndIndex );  
            GridView1.DataBind();  
  
        }  
        /// <summary>  
        /// 分页查询  
        /// </summary>  
        /// <param name="intStartIndex">开始的数据编号</param>  
        /// <param name="intEndIndex">结束的数据编号</param>  
        /// <returns>表格</returns>  
        private static DataTable MyPartData(int intStartIndex,int intEndIndex)  
        {  
            SqlConnection con = new SqlConnection("server=.;database=newssystem;uid=sa;password=123456");  
            con.Open();  
            string strCmd = "select * from comment";  
            SqlCommand cmd = new SqlCommand(strCmd,con );  
            SqlDataReader sdr = cmd.ExecuteReader();  
            DataTable dt=new DataTable ();  
            dt.Load(sdr );  
            con.Close();  
            return dt;  
  
        }  
        /// <summary>  
        /// 全部查询  
        /// </summary>  
        /// <returns></returns>  
        private static DataTable MyAllData()  
        {  
            SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;password=gss123");  
            con.Open();  
            string Cmd = "select * from comment";  
            SqlCommand cmd = new SqlCommand(Cmd, con);  
            SqlDataReader sdr = cmd.ExecuteReader();  
            DataTable dt = new DataTable();  
            dt.Load(sdr);  
            con.Close();  
            return dt;  
  
        }  
    }  

     经过这段学习我了解了关于真假分页的事儿所以,以后还要继续学习他的一些东西,还有我明白了有时候自己走不通的时候可以看看别人是怎么做的,但是不要完全的借鉴,也要有自己的体会。