惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
博客园_首页
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
Jina AI
Jina AI
月光博客
月光博客
小众软件
小众软件
S
SegmentFault 最新的问题
量子位
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - csu02

erp asp+com 删除表的一列 更改列名 千万数量级分页存储过程 XML的五种用途~ 表的列数 列名 得到一个省份,城市数据库 window.open()的所有参数列表 存儲過程解密SQL 分页控件 返回存儲過程的多個輸出字段 SqlCommand 与 SqlDataAdapter 产生一个int数组,长度为100,并向其中随机插入1-100 dataGrid使用 绑定 定向到Url 学到的程序结构 几个系统 利用IFRAME 让每个页面都继承菜单控 不是用户自定义控件来实现的 C#事件机制学习 完成单元测试
分页
csu02 · 2006-08-28 · via 博客园 - csu02

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
      }
}