两种不同情况的分页实现

(一)基于内容的分页实现:

实现功能:从数据库里取出信息条目直接显示在页面上,完成分页功能。

None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
// 在此处放置用户代码以初始化页面      
InBlock.gif
            string mySelectQuery;
InBlock.gif            
int GetReaderCount = 0;
InBlock.gif            
int totalpage;
InBlock.gif            
string nextpage = "";
InBlock.gif            
int AbsolutePage = 1;
InBlock.gif            
int i = 0;
InBlock.gif            
int icount = 0;
InBlock.gif            
int pagesize = 8;
InBlock.gif            
string brandstr; 
InBlock.gif            
string nostr;
InBlock.gif            
string c1;
InBlock.gif            
bool Hasnext = false;
InBlock.gif            
int lastnum = 0;
InBlock.gif            
int nextnum = 0;
InBlock.gif            System.Random MM 
= new System.Random(); 
InBlock.gif            System.Random NN 
= new System.Random (); 
InBlock.gif    
InBlock.gif            nextnum 
= MM.Next(1000);
InBlock.gif            lastnum 
= 0 - NN.Next(1000);
InBlock.gif    
InBlock.gif            c1 
= Request["par1"];
InBlock.gif            nextpage 
= Request["nextpage"];
InBlock.gif
InBlock.gif            SqlCommand myCommand;        
InBlock.gif            SqlDataReader myReader;
InBlock.gif            SqlConnection myConnection 
= new SqlConnection(ConfigurationSettings.AppSettings["LocalDBConn"]);            
InBlock.gif            myConnection.Open();     
InBlock.gif            
//获得信息条数
InBlock.gif
            mySelectQuery = "SELECT COUNT (*) FROM Orders";
InBlock.gif            myCommand 
= new SqlCommand(mySelectQuery, myConnection);    
InBlock.gif            myReader 
= myCommand.ExecuteReader();
InBlock.gif            
while(myReader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                GetReaderCount 
= int.Parse(myReader.GetValue(0).ToString());                
ExpandedSubBlockEnd.gif            }

InBlock.gif            myReader.Close();
InBlock.gif            totalpage 
= (GetReaderCount/8);
InBlock.gif            
if(nextpage == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                Session[
"absPage"= 1;
InBlock.gif                nextpage 
= "1";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
if(int.Parse(nextpage) > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Session[
"absPage"= int.Parse(Session["absPage"].ToString()) - 1;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(int.Parse(nextpage) < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Session[
"absPage"= int.Parse (Session["absPage"].ToString()) + 1;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            Response.Write(
"" + Session["absPage"].ToString() + "页,共" + totalpage.ToString() + "页:"+"<br/>");
InBlock.gif            Response.Write(
"------------------------"+"<br/>");
InBlock.gif            
InBlock.gif            mySelectQuery 
= "select * from Orders order by OrderID desc";
InBlock.gif            myCommand 
= new SqlCommand(mySelectQuery, myConnection);
InBlock.gif            myReader 
= myCommand.ExecuteReader();
InBlock.gif 
InBlock.gif            
while(myReader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                icount 
+= 1;
InBlock.gif                
if(icount > pagesize) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    icount 
= 1;
InBlock.gif                    AbsolutePage 
+= 1;
ExpandedSubBlockEnd.gif                }

InBlock.gif                i 
+= 1;
InBlock.gif                nostr 
= myReader["OrderID"].ToString();
InBlock.gif                brandstr 
= myReader["CustomerID"].ToString();
InBlock.gif
InBlock.gif                
if(int.Parse(Session["absPage"].ToString()) == AbsolutePage)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                
InBlock.gif                    PrintALinkBR(
"Result.aspx?par1=" + nostr, i.ToString() + ":" + brandstr);
InBlock.gif                    Hasnext 
= false;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif      
InBlock.gif                    Hasnext 
= true;
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            myReader.Close();
InBlock.gif            myConnection.Close();
InBlock.gif            Response.Write(
"" + i.ToString() + "条信息"+"<br/>");
InBlock.gif
InBlock.gif            icount 
= 0;
InBlock.gif            AbsolutePage 
= 1;
InBlock.gif            
if(Hasnext)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrintALinkBR(
"WebForm2.aspx?par1=&amp;nextpage=" + lastnum.ToString(), "下一页");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(int.Parse(Session["absPage"].ToString()) > 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrintALinkBR(
"WebForm2.aspx?par1=&amp;nextpage=" + nextnum.ToString(), "上一页");
ExpandedSubBlockEnd.gif            }

InBlock.gif
None.gif     }
None.gif
None.gif
None.gif        
public   void  PrintALinkBR( string  sHref, string  sText)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
InBlock.gif            Response.Write(
"<a title='确定' href='" + sHref + "'>" + sText + "</a><br/>");
ExpandedBlockEnd.gif        }

None.gif
None.gif



(二)在DataGrid中实现分页功能


实现功能:页面中加入DataGrid,对DataGrid实现分页功能。(添加SqlConnection和SqlCommand控件)

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.ComponentModel.Design; 
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
None.gif
namespace  DataGridPage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Data.SqlClient.SqlConnection sqlConnection1;
InBlock.gif        
protected System.Data.SqlClient.SqlCommand cmdNext;
InBlock.gif        
protected System.Data.SqlClient.SqlCommand cmdPrevious;
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnPrevious;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton btnNext;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList1;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox text1;
InBlock.gif
InBlock.gif        
private int CurrentPage;
InBlock.gif
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif            
// Put user code to initialize the page here
InBlock.gif
            if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cmdNext.Parameters[
"@customerid"].Value = "";
InBlock.gif                CurrentPage 
= 0;
InBlock.gif                FillGrid(cmdNext);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void FillGrid(System.Data.SqlClient.SqlCommand currentSqlCommand)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Data.SqlClient.SqlDataReader dr;
InBlock.gif            sqlConnection1.Open();
InBlock.gif            dr 
= currentSqlCommand.ExecuteReader();
InBlock.gif            DataGrid1.DataSource 
= dr;
InBlock.gif            DataGrid1.DataBind();
InBlock.gif            dr.Close();
InBlock.gif            sqlConnection1.Close();
InBlock.gif            ViewState[
"CurrentPage"= CurrentPage;
InBlock.gif            ViewState[CurrentPage.ToString()] 
= DataGrid1.Items[0].Cells[0].Text;
InBlock.gif            
// Determine how many rows were filled into the grid. If it is less
InBlock.gif            
// than the number of rows per page, there are no more rows in the 
InBlock.gif            
// table, and the Next button should be disabled.
InBlock.gif
            if (DataGrid1.Items.Count < DataGrid1.PageSize)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                btnNext.Enabled 
= false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//下一页
InBlock.gif
        private void btnNext_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Get the page number of the page most recently displayed
InBlock.gif
            CurrentPage = (int)(ViewState["CurrentPage"]);
InBlock.gif            CurrentPage
++;
InBlock.gif            
// Gets the id on current page
InBlock.gif
            string lastid = DataGrid1.Items[9].Cells[0].Text;
InBlock.gif            cmdNext.Parameters[
"@customerid"].Value = lastid;
InBlock.gif            FillGrid(cmdNext);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//上一页
InBlock.gif
        private void btnPrevious_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            btnNext.Enabled 
= true;
InBlock.gif            CurrentPage 
= (int)(ViewState["CurrentPage"]);
InBlock.gif            CurrentPage
--;
InBlock.gif            
if (CurrentPage >= 0
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string firstid;
InBlock.gif                firstid 
= (string)(ViewState[CurrentPage.ToString()]);
InBlock.gif                cmdPrevious.Parameters[
"@customerid"].Value = firstid;
InBlock.gif                FillGrid(cmdPrevious);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
InBlock.gif            
this.cmdNext = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.cmdPrevious = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
InBlock.gif            
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
InBlock.gif            
// 
InBlock.gif            
// sqlConnection1
InBlock.gif            
// 
InBlock.gif
            this.sqlConnection1.ConnectionString = "user id=sa;data source=localhost;initial catalog=Northwind;password=";
InBlock.gif            
// 
InBlock.gif            
// cmdNext
InBlock.gif            
// 
InBlock.gif
            this.cmdNext.CommandText = "SELECT TOP 10 CustomerID, CompanyName, City FROM Customers WHERE (CustomerID > @c" +
InBlock.gif                
"ustomerid) ORDER BY CustomerID";
InBlock.gif            
this.cmdNext.Connection = this.sqlConnection1;
InBlock.gif            
this.cmdNext.Parameters.Add(new System.Data.SqlClient.SqlParameter("@customerid", System.Data.SqlDbType.NVarChar, 5"CustomerID"));
InBlock.gif            
// 
InBlock.gif            
// cmdPrevious
InBlock.gif            
// 
InBlock.gif
            this.cmdPrevious.CommandText = "SELECT TOP 10 CustomerID, CompanyName, City FROM Customers WHERE (CustomerID >= @" +
InBlock.gif                
"customerid) ORDER BY CustomerID";
InBlock.gif            
this.cmdPrevious.Connection = this.sqlConnection1;
InBlock.gif            
this.cmdPrevious.Parameters.Add(new System.Data.SqlClient.SqlParameter("@customerid", System.Data.SqlDbType.NVarChar, 5"CustomerID"));
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/appleblossom/archive/2004/11/15/63938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值