DataGrid,DataList,Repeater简单分页

<%# DataBinder.Eval(Container.DataItem,"OrderID")%> <asp:DataList id="DataList1" style="Z-INDEX: 108; LEFT: 144px; POSITION: absolute; TOP: 72px"
  runat="server">
  <ItemTemplate>
  <FONT face="宋体">
    <%# DataBinder.Eval(Container.DataItem,"OrderID")%>
    </FONT>
    </ItemTemplate>
    </asp:DataList>



private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   SqlConnection conn=new SqlConnection("server=127.0.0.1;database=Northwind;uid=sa;pwd=;");
            SqlDataAdapter cmd=new SqlDataAdapter("select * from [orders] order by orderid desc",conn);
            DataSet ds=new DataSet();
            cmd.Fill(ds);

   PagedDataSource objPds=new PagedDataSource();
   objPds.DataSource=ds.Tables[0].DefaultView;

   objPds.AllowPaging=true;
   string recordcount=objPds.DataSourceCount.ToString();//objPds.Count.ToString(); //记录总数
   objPds.PageSize=5;
   string pagecount=objPds.PageCount.ToString(); //总页数
   Label2.Text="(共"+pagecount+"页,";
   Label3.Text=recordcount+"条)";
   int CurPage;
   if (Request.QueryString["Page"] != null)
   {
    CurPage=Convert.ToInt32(Request.QueryString["Page"]);
   }
   else
   {
    CurPage=1;
   }

   objPds.CurrentPageIndex = CurPage-1;
   Label1.Text = "第" + CurPage.ToString() + "页";

 

   if (!objPds.IsFirstPage)
   {
    HyperLink2.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
   }
   if(!objPds.IsLastPage)
   {
    HyperLink3.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage+1);
   }
   if(!objPds.IsFirstPage)
   {
    HyperLink1.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=1";
   }
   if(!objPds.IsLastPage)
   {
    HyperLink4.NavigateUrl=Request.CurrentExecutionFilePath + "?Page="+pagecount;
   }

 

   DataList1.DataSource=objPds;
   DataList1.DataBind();
   conn.Close();
  

 

  }

你可能感兴趣的:(datagrid)