⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webform1.aspx.cs

📁 本源码实现了一个简单的分页功能,从数据库中取出记录然后同控件绑定
💻 CS
字号:
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;

namespace Pagination
{
	/// <summary>
	/// WebForm1 的摘要说明。
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.LinkButton lbnPrevPage;
		protected System.Web.UI.WebControls.Label lblCurrentPage;
		protected System.Web.UI.WebControls.LinkButton lbnNextPage;
	
      
		int PageSize, PageCount, CurrentPage, RecordCount;
		MyItemList il = new MyItemList();
		ArrayList al = new ArrayList();
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			PageSize = 4;
            string connString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(Request.ApplicationPath)+"\\dbtest.mdb";
			il.ConnString=connString;
			GetPara();
			al = il.GetRecords();
			if (!Page.IsPostBack)
			{
                
				CurrentPage = 0;
				ViewState["PageIndex"] = 0;
				BindDataToDataGrid();
			}
		}

		#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

		private void GetPara()
		{
			RecordCount = il.GetCount();//得到纪录总数
			PageCount = RecordCount / PageSize;//得到总页数
			if (PageSize * PageCount != RecordCount)
				PageCount = PageCount + 1;
			ViewState["PageCount"] = PageCount;
		}
		private void BindDataToDataGrid()
		{
			int startIndex, endIndex;
			startIndex = CurrentPage * PageSize + 1;
			//endIndex = RecordCount - CurrentPage * PageSize;
			if (startIndex + 4 < RecordCount)
			{
				endIndex = startIndex + 4;

			}
			else
			{
				endIndex = RecordCount;
  
			}


			DataGrid1.DataSource = this.GetTenRecord(startIndex, endIndex);

			DataGrid1.DataBind();

			lbnNextPage.Enabled = true;
			lbnPrevPage.Enabled = true;
			if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
			if (CurrentPage == 0) lbnPrevPage.Enabled = false;
			lblCurrentPage.Text = (CurrentPage + 1).ToString();
		}

		public void Page_OnClick(Object sender, CommandEventArgs e)//点击上一页下一页按钮的事件
		{
			CurrentPage = (int)ViewState["PageIndex"];
			PageCount = (int)ViewState["PageCount"];

			string command = e.CommandName;
			switch (command)
			{
				case "next":
					if (CurrentPage < (PageCount - 1)) CurrentPage++;
					break;
				case "prev":
					if (CurrentPage > 0) CurrentPage--;
					break;
			}
			ViewState["PageIndex"] = CurrentPage;
			BindDataToDataGrid();
		}
		private DataSet GetTenRecord(int startIndex, int endIndex)
		{
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();
			dt.Columns.Add("ID");
			dt.Columns.Add("CategoryID");
			dt.Columns.Add("ItemID");
			dt.Columns.Add("ItemTitle");
			dt.Columns.Add("ItemDescription");
			dt.Columns.Add("BidsPrice");
			dt.Columns.Add("BuyPrice");
			dt.Columns.Add("ItemSubTitle");
			for (int i = startIndex - 1; i < endIndex; i++)
			{
				MyItemList mil = (MyItemList)al[i];
				DataRow row = dt.NewRow();
				row["ID"] = mil.ID.ToString();
				row["CategoryID"] = mil.CategoryID.ToString();
				row["ItemID"] = mil.ItemID.ToString();
				row["ItemTitle"] = mil.ItemTitle.ToString();
				row["ItemDescription"] = System.Web.HttpContext.Current.Server.HtmlDecode(mil.ItemDescription.ToString());
				row["BidsPrice"] = mil.BidsPrice.ToString();
				row["BuyPrice"] = mil.BuyPrice.ToString();
				row["ItemSubTitle"] = mil.ItemSubTitle.ToString();
				dt.Rows.Add(row);
			}
			ds.Tables.Add(dt);
			return ds;
		}

		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -