随笔 - 336  文章 - 78  评论 - 296  9

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

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

private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面      
            string mySelectQuery;
            
int GetReaderCount = 0;
            
int totalpage;
            
string nextpage = "";
            
int AbsolutePage = 1;
            
int i = 0;
            
int icount = 0;
            
int pagesize = 8;
            
string brandstr; 
            
string nostr;
            
string c1;
            
bool Hasnext = false;
            
int lastnum = 0;
            
int nextnum = 0;
            System.Random MM 
= new System.Random(); 
            System.Random NN 
= new System.Random (); 
    
            nextnum 
= MM.Next(1000);
            lastnum 
= 0 - NN.Next(1000);
    
            c1 
= Request["par1"];
            nextpage 
= Request["nextpage"];

            SqlCommand myCommand;        
            SqlDataReader myReader;
            SqlConnection myConnection 
= new SqlConnection(ConfigurationSettings.AppSettings["LocalDBConn"]);            
            myConnection.Open();     
            
//获得信息条数
            mySelectQuery = "SELECT COUNT (*) FROM Orders";
            myCommand 
= new SqlCommand(mySelectQuery, myConnection);    
            myReader 
= myCommand.ExecuteReader();
            
while(myReader.Read())
            
{
                GetReaderCount 
= int.Parse(myReader.GetValue(0).ToString());                
            }

            myReader.Close();
            totalpage 
= (GetReaderCount/8);
            
if(nextpage == null)
            

                Session[
"absPage"= 1;
                nextpage 
= "1";
            }

            
else
            

                
if(int.Parse(nextpage) > 0)
                
{
                    Session[
"absPage"= int.Parse(Session["absPage"].ToString()) - 1;
                }

                
if(int.Parse(nextpage) < 0)
                
{
                    Session[
"absPage"= int.Parse (Session["absPage"].ToString()) + 1;
                }

            }

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

                i 
+= 1;
                nostr 
= myReader["OrderID"].ToString();
                brandstr 
= myReader["CustomerID"].ToString();

                
if(int.Parse(Session["absPage"].ToString()) == AbsolutePage)
                
{                
                    PrintALinkBR(
"Result.aspx?par1=" + nostr, i.ToString() + ":" + brandstr);
                    Hasnext 
= false;
                }

                
else
                
{
      
                    Hasnext 
= true;
                    
                }


            }

            myReader.Close();
            myConnection.Close();
            Response.Write(
"" + i.ToString() + "条信息"+"<br/>");

            icount 
= 0;
            AbsolutePage 
= 1;
            
if(Hasnext)
            
{
                PrintALinkBR(
"WebForm2.aspx?par1=&amp;nextpage=" + lastnum.ToString(), "下一页");
            }

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

    }


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




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


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

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design; 
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace DataGridPage
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Data.SqlClient.SqlConnection sqlConnection1;
        
protected System.Data.SqlClient.SqlCommand cmdNext;
        
protected System.Data.SqlClient.SqlCommand cmdPrevious;
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
        
protected System.Web.UI.WebControls.LinkButton btnPrevious;
        
protected System.Web.UI.WebControls.LinkButton btnNext;
        
protected System.Web.UI.WebControls.DropDownList DropDownList1;
        
protected System.Web.UI.WebControls.TextBox text1;

        
private int CurrentPage;

    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            
// Put user code to initialize the page here
            if (!Page.IsPostBack)
            
{
                cmdNext.Parameters[
"@customerid"].Value = "";
                CurrentPage 
= 0;
                FillGrid(cmdNext);
            }


        }


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

        }


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



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

        }




        



        
Web 窗体设计器生成的代码
    }

}

posted on 2004-11-15 10:23  apple  阅读(...)  评论(...编辑  收藏