.net 高效率分頁解決方案(代碼部分)

來源:互聯網
上載者:User

private void Page_Load(object sender, System.EventArgs e)
  {
   this.btnPreviousPage.Click += new System.EventHandler(this.NavigateToPage);
   this.btnNextPage.Click += new System.EventHandler(this.NavigateToPage);
   if(!IsPostBack)
   {
    StartIndex = 0;
    SqlConnection MyConnection=new SqlConnection("Data Source=NGB-INTRATST-1;User ID=Winc;Password=123;initial catalog=Winc;");
    SqlCommand MyCommand = new SqlCommand("Select num=COUNT(*) from Authors",MyConnection);
    MyConnection.Open();
    SqlDataReader dr = MyCommand.ExecuteReader(CommandBehavior.SingleRow);
    
     if(dr.Read())
      MyDataGrid.VirtualItemCount = (int)dr["num"];
    
     dr.Close();
     MyConnection.Close();
     BindGridToSource(StartIndex,"上一頁");
   }
  }

------------------------------------------------------------------

private void BindGridToSource(int StartPosition,string GoToPage)
  {
   SqlConnection MyConnection=new SqlConnection("Data Source=NGB-INTRATST-1;User ID=Winc;Password=123;initial catalog=Winc;");
   SqlCommand MyCommand = null;
   switch(GoToPage)
   {
    case "上一頁":
     MyCommand = new SqlCommand("Select Top 5 au_id,au_lname,au_fname," +
      "phone,address,city,state,zip,contract from Authors" +" "+
      "Where au_id >= @uid order by au_id",MyConnection);
     
     if(StartPosition == 0)
      MyCommand.Parameters.Add("@uid",SqlDbType.NVarChar,4).Value = "0";
     else
      MyCommand.Parameters.Add("@uid",SqlDbType.NVarChar,4).Value =
      ViewState[(MyDataGrid.CurrentPageIndex+1).ToString()];
    break;

    case "下一頁":
     MyCommand = new SqlCommand("Select Top 5 au_id,au_lname,au_fname," +
      "phone,address,city,state,zip,contract from Authors" + " "+
      "Where au_id>@uid order by au_id",MyConnection);
     MyCommand.Parameters.Add("@uid",SqlDbType.NVarChar,4).Value =
      MyDataGrid.Items[4].Cells[0].Text;

    break;
   }

   MyConnection.Open();

    SqlDataReader dr = MyCommand.ExecuteReader();
    MyDataGrid.DataSource = dr;
    MyDataGrid.DataBind();
    dr.Close();
    MyConnection.Close();

   ViewState[(MyDataGrid.CurrentPageIndex+1).ToString()] =
    MyDataGrid.Items[0].Cells[0].Text;
  }

-----------------------------------------------------------------------

public void NavigateToPage(object sender,System.EventArgs e)
  {
   string pageInfo = ((Button)sender).CommandName;
   switch (pageInfo)
   {
    case "上一頁":
      if(MyDataGrid.CurrentPageIndex > 0)
       MyDataGrid.CurrentPageIndex -= 1;
      else
       return;
     break;
 
    case "下一頁":
     if(MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
     MyDataGrid.CurrentPageIndex += 1;
     else
      return;
     break;
   }

    StartIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
    BindGridToSource(StartIndex,pageInfo);

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.