小 牛

博客园 首页 新随笔 联系 订阅 管理

using System;
using System.Collections;
using System.ComponentModel;
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;
using System.Data.SqlClient;
using System.Configuration;

namespace mystore.Admin
{
      /// <summary>
      /// AllSupply 的摘要说明。
      /// </summary>
      public class AllSupply : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Label lblRecordCount;
            protected System.Web.UI.WebControls.Label lblCurrentPage;
            protected System.Web.UI.WebControls.Label lblPageCount;
            protected System.Web.UI.WebControls.LinkButton lbnFirstPage;
            protected System.Web.UI.WebControls.LinkButton lbnPrevPage;
            protected System.Web.UI.WebControls.LinkButton lbnNextPage;
            protected System.Web.UI.WebControls.LinkButton lbnLatePage;
            public int PageSize;
            public int CurrentPage;
            public int RecordCount;
            public int PageCount;
            protected System.Web.UI.WebControls.DataList MyList;
            SqlConnection myConn;

           
            private void Page_Load(object sender, System.EventArgs e)
            {   myConn=new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
                  // 在此处放置用户代码以初始化页面
                  PageSize=20;
                  //myConnection.Open();
                  if(!Page.IsPostBack)
                  {
                        ListBind();
                        CurrentPage = 0;
                        ViewState["PageIndex"] = 0;
                        //计算总共有多少记录
                        RecordCount = CalculateRecord();
                        lblRecordCount.Text = RecordCount.ToString();
                        //计算总共有多少页
                        if (RecordCount%PageSize==0)
                              PageCount = RecordCount/PageSize;
                        else
                              PageCount = (RecordCount/PageSize)+1;
                        lblPageCount.Text = PageCount.ToString();
                        ViewState["PageCount"] = PageCount;
                  }

            }

            //计算总共有多少条记录
            public int CalculateRecord()
            {
                  int intCount;
                  //
                  string strCount = "select count(*) as co from suppliers ";
                  SqlCommand MyComm = new SqlCommand (strCount,myConn);
                  myConn.Open();
                  SqlDataReader dr = MyComm.ExecuteReader();
                  if(dr.Read())
                  {
                        intCount = Int32.Parse(dr["co"].ToString());
                  }
                  else
                  {
                        intCount = 0;
                  }
                  dr.Close();
                  myConn.Close();
                  return intCount;
            }

            ICollection CreateSource()
            {
                 
                  int StartIndex;
                  //设定导入的起终地址
                  StartIndex = CurrentPage*PageSize;
                  //myConnection.Open();
                  string strSel = "select * from Suppliers order by supplierid";
                  DataSet ds = new DataSet();
                  SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,myConn);
                  MyAdapter.Fill(ds,StartIndex,PageSize,"products");
                  //myConnection.Close();
                  return ds.Tables["products"].DefaultView;
                 
            }

            public void ListBind()
            {
                  MyList.DataSource = CreateSource();
                  MyList.DataBind();
                  lbnFirstPage.Enabled = true;
                  lbnNextPage.Enabled = true;
                  lbnPrevPage.Enabled = true;
                  lbnLatePage.Enabled = true;
                  if(CurrentPage==(PageCount-1))
                  {
                              lbnNextPage.Enabled = false;
                        lbnLatePage.Enabled = false;}
                  if(CurrentPage==0)
                  {
                              lbnPrevPage.Enabled = false;
                        lbnFirstPage.Enabled = false;}
                  lblCurrentPage.Text = (CurrentPage+1).ToString();
            }

            public void Page_OnClick(Object sender,CommandEventArgs e)
            {
                  CurrentPage = (int)ViewState["PageIndex"];
                  PageCount = (int)ViewState["PageCount"];
                  string cmd = e.CommandName;
                  Response.Write (cmd);
                  //判断cmd,以判定翻页方向
                  switch(cmd)
                  {
                        case "first":
                              CurrentPage=0;Response.Write (CurrentPage);Response.Write ("a");
                              break;
                       
                        case "prev":
                              if(CurrentPage>0) CurrentPage--;Response.Write (CurrentPage);Response.Write ("b");
                              break;
                        case "next":
                              if(CurrentPage<(PageCount-1)) CurrentPage++;Response.Write (CurrentPage);Response.Write ("c");
                              break;
                        case "late":
                              CurrentPage=PageCount-1;Response.Write (CurrentPage);Response.Write (PageCount);Response.Write ("d");
                              break;
                       
                  }
                  ViewState["PageIndex"] = CurrentPage;
                  ListBind();
            }

            #region Web 窗体设计器生成的代码
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
           
            /// <summary>
            /// 设计器支持所需的方法 - 不要使用代码编辑器修改
            /// 此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {   
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }
}

posted on 2006-08-28 14:34  csu02  阅读(358)  评论(0编辑  收藏  举报