.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;

using System.Data.SqlClient;
namespace WebApplication1
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnLast;
protected System.Web.UI.WebControls.CheckBox chk1;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.Label lblPageCount;

ICollection CreateDataSource() 

// System.Data.SqlClient.SqlDataAdapter  
/* 
读取数据库的信息,获得DataView 
*/ 
SqlConnection MyConnection = new SqlConnection("data source=172.16.36.222;initial catalog=RemoteEdu;password=1234567890;persist s" +
"ecurity info=True;user id=sa;workstation id=BAIHAO;packet size=4096"); 
SqlCommand MyDataSetCommand = new SqlCommand("SELECT GroupID, GroupName, Brief, RegistDate, GroupState, InvalidDate, Deleteable" +
" FROM GroupInfo",MyConnection); 
DataSet ds= new DataSet(); 
SqlDataAdapter ada = new SqlDataAdapter(MyDataSetCommand); //
ada.Fill(ds,"admin_enter"); 
return ds.Tables["admin_enter"].DefaultView; 
}

//然后中是Page_Load函数,在这里主要是判断一下是否显示DataGrid自带的那些分页数字,使用的是PageStyle的Visible属性:

void Page_Load(Object sender, EventArgs e) 

//判断是否隐藏PagerStyle-Mode 
if (chk1.Checked) 

MyDataGrid.PagerStyle.Visible=true; 

else 

MyDataGrid.PagerStyle.Visible=false; 
}

BindGrid();

}

//下面是处理点击事件的PagerButtonClick,这是我们的核心部分,其实我们操作的也只是DataGrid的CurrentPageIndex属性。如果CurrentPageIndex小于PageCount则有下一页,如果CurrentPageIndex大于0则表示有前一页。

protected void PagerButtonClick(Object sender, EventArgs e) 

//获得LinkButton的参数值 
String arg = ((LinkButton)sender).CommandArgument;

switch(arg) 

case ("next"): 
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) 
MyDataGrid.CurrentPageIndex ++; 
break; 
case ("prev"): 
if (MyDataGrid.CurrentPageIndex > 0) 
MyDataGrid.CurrentPageIndex --; 
break; 
case ("last"): 
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1); 
break; 
default: 
//本页值 
MyDataGrid.CurrentPageIndex = Int32.Parse(arg); 
break; 

BindGrid(); 
}

//下面是MyDataGrid_Page,主要操作是调用BindGrid函数,以将数据交给DataGrid显示:

protected void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e) 

//处理按下数字的方法 
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid(); 
}

//最后是两个函数,他们的作用,我都注释了:)

void BindGrid() 

//将DataView绑定到DataGrid上去 
MyDataGrid.DataSource = CreateDataSource(); 
MyDataGrid.DataBind(); 
ShowStats(); 
}

void ShowStats() 

//显示页面信息 
lblCurrentIndex.Text = "当前页数为: " + ((int)MyDataGrid.CurrentPageIndex+1); 
lblPageCount.Text = "总页数是: " + MyDataGrid.PageCount; 
}

#region Web Form Designer generated code
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
}
}

最新文章

  1. SQL Server 修改表结构后无法保存的老问题
  2. 在Java中如何实现“Pless presss any key to continue.”
  3. Mysql查询阻塞初探
  4. 在项目中那个少用if else 语句,精简代码,便于维护的方法(1)
  5. 配置ogg目录索引-oracle与mysql的双向同步步骤
  6. sql数据库 管理处理问题--维护计划
  7. 将插入的新行放入dataGridView的第一行
  8. 怎么实现元素ol的降序排序显示
  9. 【原创】记一次Sql2008R2的数据库订阅发布遇到的问题!
  10. linux在线学习
  11. NYOJ-228 士兵杀敌5
  12. 漫谈CGI FastCGI WSGI
  13. [Codeforces670A]Holidays(数学,构造)
  14. Hadoop-1.1.2、HBase-0.94.7完全分布式集群结构
  15. android 控件注意点
  16. PL\SQL学习笔记
  17. css中的几个小tip(二)
  18. 201671010133 2016-2017-2 《java程序设计》 初学java!
  19. 使用Spring+MySql实现读写分离(三)主从复制
  20. element-ui中 table表格hover 修改背景色

热门文章

  1. AIM Tech Round (Div. 2) A. Save Luke 水题
  2. Spark1.0.0 开发环境高速搭建
  3. leanchat-android
  4. 剑指 offer set 8 树的子结构
  5. 0c-40-ARC下多对象内存管理
  6. CPU 100%
  7. systemtap-与 oracle 转
  8. Game: Map Design Considerations 游戏地图设计指南
  9. nie题目-游戏排行榜设计
  10. The Kernel Newbie Corner: Kernel Debugging Using proc &quot;Sequence&quot; Files--Part 1