C#版本 例子
1
<%
@ Page language
=
"
c#
"
EnableViewState
=
"
true
"
Codebehind
=
"
DataGridPaging.aspx.cs
"
2 AutoEventWireup = " false " Inherits = " eMeng.Exam.DataGridPaging.DataGridPaging " %>
3 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
4 < HTML >
5 < HEAD >
6 < meta content ="Visual Basic 7.0" name ="CODE_LANGUAGE" >
7 < meta content ="JavaScript" name ="vs_defaultClientScript" >
8 < meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
9 </ HEAD >
10 < body MS_POSITIONING ="GridLayout" >
11 < form id ="Form1" runat ="server" >
12 < asp:datagrid id ="MyDataGrid" runat ="server" AutoGenerateColumns ="False"
13 HorizontalAlign ="Center" AlternatingItemStyle-BackColor ="#eeeeee"
14 HeaderStyle-BackColor ="#aaaadd" Font-Size ="8pt" Font-Name ="Verdana"
15 CellSpacing ="0" CellPadding ="3" GridLines ="Both" BorderWidth ="1"
16 BorderColor ="black" OnPageIndexChanged ="MyDataGrid_Page" PagerStyle-HorizontalAlign ="Right"
17 PagerStyle-Mode ="NumericPages" PageSize ="5" AllowPaging ="True" >
18 < AlternatingItemStyle BackColor ="#EEEEEE" ></ AlternatingItemStyle >
19 < HeaderStyle BackColor ="#AAAADD" Font-Bold ="True" HorizontalAlign ="Center" ></ HeaderStyle >
20 < PagerStyle HorizontalAlign ="Right" Mode ="NumericPages" ></ PagerStyle >
21 < Columns >
22 < asp:BoundColumn HeaderText ="标题" DataField ="Title" HeaderStyle-Width ="480px" >
23 </ asp:BoundColumn >
24 < asp:BoundColumn HeaderText ="发表日期" DataField ="CreateDate" DataFormatString ="{0:yyyy-MM-dd hh:mm:ss}" >
25 </ asp:BoundColumn >
26 </ Columns >
27 </ asp:datagrid >
28 < p style ="FONT-SIZE:9pt" align ="center" >
29 < asp:label id ="lblPageCount" runat ="server" ></ asp:label >
30 < asp:label id ="lblCurrentIndex" runat ="server" ></ asp:label >
31 < asp:linkbutton id ="btnFirst" onclick ="PagerButtonClick" runat ="server" Font-Name ="verdana"
32 Font-size ="8pt" ForeColor ="navy" CommandArgument ="0" ></ asp:linkbutton >
33 < asp:linkbutton id ="btnPrev" onclick ="PagerButtonClick" runat ="server" Font-Name ="verdana"
34 Font-size ="8pt" ForeColor ="navy" CommandArgument ="prev" ></ asp:linkbutton >
35 < asp:linkbutton id ="btnNext" onclick ="PagerButtonClick" runat ="server" Font-Name ="verdana"
36 Font-size ="8pt" ForeColor ="navy" CommandArgument ="next" ></ asp:linkbutton >
37 < asp:linkbutton id ="btnLast" onclick ="PagerButtonClick" runat ="server" Font-Name ="verdana"
38 Font-size ="8pt" ForeColor ="navy" CommandArgument ="last" ></ asp:linkbutton >
39 </ p >
40 </ form >
41 </ body >
42 </ HTML >
2 AutoEventWireup = " false " Inherits = " eMeng.Exam.DataGridPaging.DataGridPaging " %>
3 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
4 < HTML >
5 < HEAD >
6 < meta content ="Visual Basic 7.0" name ="CODE_LANGUAGE" >
7 < meta content ="JavaScript" name ="vs_defaultClientScript" >
8 < meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
9 </ HEAD >
10 < body MS_POSITIONING ="GridLayout" >
11 < form id ="Form1" runat ="server" >
12 < asp:datagrid id ="MyDataGrid" runat ="server" AutoGenerateColumns ="False"
13 HorizontalAlign ="Center" AlternatingItemStyle-BackColor ="#eeeeee"
14 HeaderStyle-BackColor ="#aaaadd" Font-Size ="8pt" Font-Name ="Verdana"
15 CellSpacing ="0" CellPadding ="3" GridLines ="Both" BorderWidth ="1"
16 BorderColor ="black" OnPageIndexChanged ="MyDataGrid_Page" PagerStyle-HorizontalAlign ="Right"
17 PagerStyle-Mode ="NumericPages" PageSize ="5" AllowPaging ="True" >
18 < AlternatingItemStyle BackColor ="#EEEEEE" ></ AlternatingItemStyle >
19 < HeaderStyle BackColor ="#AAAADD" Font-Bold ="True" HorizontalAlign ="Center" ></ HeaderStyle >
20 < PagerStyle HorizontalAlign ="Right" Mode ="NumericPages" ></ PagerStyle >
21 < Columns >
22 < asp:BoundColumn HeaderText ="标题" DataField ="Title" HeaderStyle-Width ="480px" >
23 </ asp:BoundColumn >
24 < asp:BoundColumn HeaderText ="发表日期" DataField ="CreateDate" DataFormatString ="{0:yyyy-MM-dd hh:mm:ss}" >
25 </ asp:BoundColumn >
26 </ Columns >
27 </ asp:datagrid >
28 < p style ="FONT-SIZE:9pt" align ="center" >
29 < asp:label id ="lblPageCount" runat ="server" ></ asp:label >
30 < asp:label id ="lblCurrentIndex" runat ="server" ></ asp:label >
31 < asp:linkbutton id ="btnFirst" onclick ="PagerButtonClick" runat ="server" Font-Name ="verdana"
32 Font-size ="8pt" ForeColor ="navy" CommandArgument ="0" ></ asp:linkbutton >
33 < asp:linkbutton id ="btnPrev" onclick ="PagerButtonClick" runat ="server" Font-Name ="verdana"
34 Font-size ="8pt" ForeColor ="navy" CommandArgument ="prev" ></ asp:linkbutton >
35 < asp:linkbutton id ="btnNext" onclick ="PagerButtonClick" runat ="server" Font-Name ="verdana"
36 Font-size ="8pt" ForeColor ="navy" CommandArgument ="next" ></ asp:linkbutton >
37 < asp:linkbutton id ="btnLast" onclick ="PagerButtonClick" runat ="server" Font-Name ="verdana"
38 Font-size ="8pt" ForeColor ="navy" CommandArgument ="last" ></ asp:linkbutton >
39 </ p >
40 </ form >
41 </ body >
42 </ HTML >
DataGridPaging.aspx.cs
1
using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Data.OleDb;
6 using System.Drawing;
7 using System.Web;
8 using System.Web.SessionState;
9 using System.Web.UI;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.HtmlControls;
12
13 namespace eMeng.Exam.DataGridPaging
14 {
15 /// <summary>
16 /// DataGridPaging 的摘要说明。
17 /// </summary>
18 public class DataGridPaging : System.Web.UI.Page
19 {
20 protected System.Web.UI.WebControls.DataGrid MyDataGrid;
21 protected System.Web.UI.WebControls.Label lblPageCount;
22 protected System.Web.UI.WebControls.Label lblCurrentIndex;
23 protected System.Web.UI.WebControls.LinkButton btnFirst;
24 protected System.Web.UI.WebControls.LinkButton btnPrev;
25 protected System.Web.UI.WebControls.LinkButton btnNext;
26 protected System.Web.UI.WebControls.LinkButton btnLast;
27 private OleDbConnection cn = new OleDbConnection();
28
29 private void Page_Load( object sender, System.EventArgs e)
30 {
31 // 在此处放置用户代码以初始化页面
32 btnFirst.Text = " 最首页 ";
33 btnPrev.Text = " 前一页 ";
34 btnNext.Text = " 下一页 ";
35 btnLast.Text = " 最后页 ";
36 OpenDatabase();
37 BindGrid();
38 }
39 private void OpenDatabase()
40 {
41 cn.ConnectionString = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Server.MapPath( " xxxx.mdb ");
42 cn.Open();
43 }
44 private void ShowStats()
45 {
46 lblCurrentIndex.Text = " 第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页 ";
47 lblPageCount.Text = " 总共 " + MyDataGrid.PageCount.ToString() + " 页 ";
48 }
49
50 public void PagerButtonClick( object sender, EventArgs e)
51 {
52 string arg = ((LinkButton)sender).CommandArgument.ToString();
53 switch(arg)
54 {
55 case " next ":
56 if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
57 {
58 MyDataGrid.CurrentPageIndex += 1;
59 }
60 break;
61 case " prev ":
62 if (MyDataGrid.CurrentPageIndex > 0)
63 {
64 MyDataGrid.CurrentPageIndex -= 1;
65 }
66 break;
67 case " last ":
68 MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
69 break;
70 default:
71 MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
72 break;
73 }
74 BindGrid();
75 ShowStats();
76 }
77 public void BindGrid()
78 {
79 OleDbConnection myConnection = cn;
80 DataSet ds = new DataSet();
81 OleDbDataAdapter adapter = new OleDbDataAdapter( " Select Title,CreateDate from Document ", myConnection);
82 adapter.Fill(ds, " Document ");
83 MyDataGrid.DataSource = ds.Tables[ " Document "].DefaultView;
84 MyDataGrid.DataBind();
85 ShowStats();
86 }
87 public void MyDataGrid_Page( object sender, DataGridPageChangedEventArgs e)
88 {
89 int startIndex ;
90 startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
91 MyDataGrid.CurrentPageIndex = e.NewPageIndex;
92 BindGrid();
93 ShowStats();
94 }
95
96 #region Web Form Designer generated code
97 override protected void OnInit(EventArgs e)
98 {
99 //
100 // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
101 //
102 InitializeComponent();
103 base.OnInit(e);
104 }
105
106 /// <summary>
107 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
108 /// 此方法的内容。
109 /// </summary>
110 private void InitializeComponent()
111 {
112 this.Load += new System.EventHandler( this.Page_Load);
113
114 }
115 #endregion
116 }
117 }
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Data.OleDb;
6 using System.Drawing;
7 using System.Web;
8 using System.Web.SessionState;
9 using System.Web.UI;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.HtmlControls;
12
13 namespace eMeng.Exam.DataGridPaging
14 {
15 /// <summary>
16 /// DataGridPaging 的摘要说明。
17 /// </summary>
18 public class DataGridPaging : System.Web.UI.Page
19 {
20 protected System.Web.UI.WebControls.DataGrid MyDataGrid;
21 protected System.Web.UI.WebControls.Label lblPageCount;
22 protected System.Web.UI.WebControls.Label lblCurrentIndex;
23 protected System.Web.UI.WebControls.LinkButton btnFirst;
24 protected System.Web.UI.WebControls.LinkButton btnPrev;
25 protected System.Web.UI.WebControls.LinkButton btnNext;
26 protected System.Web.UI.WebControls.LinkButton btnLast;
27 private OleDbConnection cn = new OleDbConnection();
28
29 private void Page_Load( object sender, System.EventArgs e)
30 {
31 // 在此处放置用户代码以初始化页面
32 btnFirst.Text = " 最首页 ";
33 btnPrev.Text = " 前一页 ";
34 btnNext.Text = " 下一页 ";
35 btnLast.Text = " 最后页 ";
36 OpenDatabase();
37 BindGrid();
38 }
39 private void OpenDatabase()
40 {
41 cn.ConnectionString = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Server.MapPath( " xxxx.mdb ");
42 cn.Open();
43 }
44 private void ShowStats()
45 {
46 lblCurrentIndex.Text = " 第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页 ";
47 lblPageCount.Text = " 总共 " + MyDataGrid.PageCount.ToString() + " 页 ";
48 }
49
50 public void PagerButtonClick( object sender, EventArgs e)
51 {
52 string arg = ((LinkButton)sender).CommandArgument.ToString();
53 switch(arg)
54 {
55 case " next ":
56 if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
57 {
58 MyDataGrid.CurrentPageIndex += 1;
59 }
60 break;
61 case " prev ":
62 if (MyDataGrid.CurrentPageIndex > 0)
63 {
64 MyDataGrid.CurrentPageIndex -= 1;
65 }
66 break;
67 case " last ":
68 MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
69 break;
70 default:
71 MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
72 break;
73 }
74 BindGrid();
75 ShowStats();
76 }
77 public void BindGrid()
78 {
79 OleDbConnection myConnection = cn;
80 DataSet ds = new DataSet();
81 OleDbDataAdapter adapter = new OleDbDataAdapter( " Select Title,CreateDate from Document ", myConnection);
82 adapter.Fill(ds, " Document ");
83 MyDataGrid.DataSource = ds.Tables[ " Document "].DefaultView;
84 MyDataGrid.DataBind();
85 ShowStats();
86 }
87 public void MyDataGrid_Page( object sender, DataGridPageChangedEventArgs e)
88 {
89 int startIndex ;
90 startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
91 MyDataGrid.CurrentPageIndex = e.NewPageIndex;
92 BindGrid();
93 ShowStats();
94 }
95
96 #region Web Form Designer generated code
97 override protected void OnInit(EventArgs e)
98 {
99 //
100 // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
101 //
102 InitializeComponent();
103 base.OnInit(e);
104 }
105
106 /// <summary>
107 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
108 /// 此方法的内容。
109 /// </summary>
110 private void InitializeComponent()
111 {
112 this.Load += new System.EventHandler( this.Page_Load);
113
114 }
115 #endregion
116 }
117 }
方法2:完全后代码写法:例子
DataGridPaging2.aspx
1
<%
@ Page language
=
"
c#
"
EnableViewState
=
"
true
"
Codebehind
=
"
DataGridPaging2.aspx.cs
"
AutoEventWireup
=
"
false
"
2 Inherits = " eMeng.Exam.DataGridPaging2.DataGridPaging2 " %>
3 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
4 < HTML >
5 < HEAD >
6 < TITLE >功能齐全的分页的例子【2】 </ TITLE >
7 < meta content ="Visual Basic 7.0" name ="CODE_LANGUAGE" >
8 < meta content ="JavaScript" name ="vs_defaultClientScript" >
9 < meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
10 </ HEAD >
11 < body MS_POSITIONING ="GridLayout" >
12 < form id ="Form1" runat ="server" >
13 < asp:datagrid id ="MyDataGrid" runat ="server" AutoGenerateColumns ="False" HorizontalAlign ="Center"
14 AlternatingItemStyle-BackColor ="#eeeeee" HeaderStyle-BackColor ="#aaaadd" Font-Size ="8pt"
15 Font-Name ="Verdana" CellSpacing ="0" CellPadding ="3" GridLines ="Both" BorderWidth ="1"
16 BorderColor ="black" PagerStyle-HorizontalAlign ="Right" PagerStyle-Mode ="NumericPages"
17 PageSize ="5" AllowPaging ="True" >
18 < AlternatingItemStyle BackColor ="#EEEEEE" ></ AlternatingItemStyle >
19 < HeaderStyle BackColor ="#AAAADD" Font-Bold ="True" HorizontalAlign ="Center" ></ HeaderStyle >
20 < PagerStyle HorizontalAlign ="Right" Mode ="NumericPages" ></ PagerStyle >
21 < Columns >
22 < asp:BoundColumn HeaderText ="标题" DataField ="Title" HeaderStyle-Width ="480px" ></ asp:BoundColumn >
23 < asp:BoundColumn HeaderText ="发表日期" DataField ="CreateDate"
24 DataFormatString ="{0:yyyy-MM-dd hh:mm:ss}" ></ asp:BoundColumn >
25 </ Columns >
26 </ asp:datagrid >
27 < p align ="center" >
28 < asp:label id ="lblPageCount" runat ="server" Font-Size ="9pt" Font-Bold ="True" ></ asp:label >
29 < asp:label id ="lblCurrentIndex" runat ="server" Font-Size ="9pt" Font-Bold ="True" ></ asp:label >
30 < asp:linkbutton id ="btnFirst" runat ="server" Font-size ="9pt" ForeColor ="navy" CommandArgument ="0" />
31 < asp:linkbutton id ="btnPrev" runat ="server" Font-size ="9pt" ForeColor ="navy" CommandArgument ="prev" />
32 < asp:linkbutton id ="btnNext" runat ="server" Font-size ="9pt" ForeColor ="navy" CommandArgument ="next" />
33 < asp:linkbutton id ="btnLast" runat ="server" Font-size ="9pt" ForeColor ="navy" CommandArgument ="last" />
34 </ p >
35 </ form >
36 </ body >
37 </ HTML >
2 Inherits = " eMeng.Exam.DataGridPaging2.DataGridPaging2 " %>
3 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
4 < HTML >
5 < HEAD >
6 < TITLE >功能齐全的分页的例子【2】 </ TITLE >
7 < meta content ="Visual Basic 7.0" name ="CODE_LANGUAGE" >
8 < meta content ="JavaScript" name ="vs_defaultClientScript" >
9 < meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
10 </ HEAD >
11 < body MS_POSITIONING ="GridLayout" >
12 < form id ="Form1" runat ="server" >
13 < asp:datagrid id ="MyDataGrid" runat ="server" AutoGenerateColumns ="False" HorizontalAlign ="Center"
14 AlternatingItemStyle-BackColor ="#eeeeee" HeaderStyle-BackColor ="#aaaadd" Font-Size ="8pt"
15 Font-Name ="Verdana" CellSpacing ="0" CellPadding ="3" GridLines ="Both" BorderWidth ="1"
16 BorderColor ="black" PagerStyle-HorizontalAlign ="Right" PagerStyle-Mode ="NumericPages"
17 PageSize ="5" AllowPaging ="True" >
18 < AlternatingItemStyle BackColor ="#EEEEEE" ></ AlternatingItemStyle >
19 < HeaderStyle BackColor ="#AAAADD" Font-Bold ="True" HorizontalAlign ="Center" ></ HeaderStyle >
20 < PagerStyle HorizontalAlign ="Right" Mode ="NumericPages" ></ PagerStyle >
21 < Columns >
22 < asp:BoundColumn HeaderText ="标题" DataField ="Title" HeaderStyle-Width ="480px" ></ asp:BoundColumn >
23 < asp:BoundColumn HeaderText ="发表日期" DataField ="CreateDate"
24 DataFormatString ="{0:yyyy-MM-dd hh:mm:ss}" ></ asp:BoundColumn >
25 </ Columns >
26 </ asp:datagrid >
27 < p align ="center" >
28 < asp:label id ="lblPageCount" runat ="server" Font-Size ="9pt" Font-Bold ="True" ></ asp:label >
29 < asp:label id ="lblCurrentIndex" runat ="server" Font-Size ="9pt" Font-Bold ="True" ></ asp:label >
30 < asp:linkbutton id ="btnFirst" runat ="server" Font-size ="9pt" ForeColor ="navy" CommandArgument ="0" />
31 < asp:linkbutton id ="btnPrev" runat ="server" Font-size ="9pt" ForeColor ="navy" CommandArgument ="prev" />
32 < asp:linkbutton id ="btnNext" runat ="server" Font-size ="9pt" ForeColor ="navy" CommandArgument ="next" />
33 < asp:linkbutton id ="btnLast" runat ="server" Font-size ="9pt" ForeColor ="navy" CommandArgument ="last" />
34 </ p >
35 </ form >
36 </ body >
37 </ HTML >
DataGridPaging2.aspx.cs
1
using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Data.OleDb;
6 using System.Drawing;
7 using System.Web;
8 using System.Web.SessionState;
9 using System.Web.UI;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.HtmlControls;
12
13 namespace eMeng.Exam.DataGridPaging2
14 {
15 /// <summary>
16 /// DataGridPaging 的摘要说明。
17 /// </summary>
18 public class DataGridPaging2 : System.Web.UI.Page
19 {
20 protected System.Web.UI.WebControls.DataGrid MyDataGrid;
21 protected System.Web.UI.WebControls.Label lblPageCount;
22 protected System.Web.UI.WebControls.Label lblCurrentIndex;
23 protected System.Web.UI.WebControls.LinkButton btnFirst;
24 protected System.Web.UI.WebControls.LinkButton btnPrev;
25 protected System.Web.UI.WebControls.LinkButton btnNext;
26 protected System.Web.UI.WebControls.LinkButton btnLast;
27 private OleDbConnection cn = new OleDbConnection();
28
29 private void Page_Load( object sender, System.EventArgs e)
30 {
31 // 在此处放置用户代码以初始化页面
32 btnFirst.Text = " 最首页 ";
33 btnPrev.Text = " 前一页 ";
34 btnNext.Text = " 下一页 ";
35 btnLast.Text = " 最后页 ";
36 OpenDatabase();
37 BindGrid();
38 }
39 private void OpenDatabase()
40 {
41 cn.ConnectionString = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "
42 + HttpContext.Current.Server.MapPath( " http://www.cnblogs.com/aspxWeb.mdb.ascx ");
43 cn.Open();
44 }
45 private void ShowStats()
46 {
47 lblCurrentIndex.Text = " 第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页 ";
48 lblPageCount.Text = " 总共 " + MyDataGrid.PageCount.ToString() + " 页 ";
49 }
50
51 public void PagerButtonClick( object sender, EventArgs e)
52 {
53 string arg = ((LinkButton)sender).CommandArgument.ToString();
54 switch(arg)
55 {
56 case " next ":
57 if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
58 {
59 MyDataGrid.CurrentPageIndex += 1;
60 }
61 break;
62 case " prev ":
63 if (MyDataGrid.CurrentPageIndex > 0)
64 {
65 MyDataGrid.CurrentPageIndex -= 1;
66 }
67 break;
68 case " last ":
69 MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
70 break;
71 default:
72 MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
73 break;
74 }
75 BindGrid();
76 ShowStats();
77 }
78 public void BindGrid()
79 {
80 OleDbConnection myConnection = cn;
81 DataSet ds = new DataSet();
82 OleDbDataAdapter adapter = new OleDbDataAdapter( " Select Title,CreateDate from Document ", myConnection);
83 adapter.Fill(ds, " Document ");
84 MyDataGrid.DataSource = ds.Tables[ " Document "].DefaultView;
85 MyDataGrid.DataBind();
86 ShowStats();
87 }
88
89 #region Web Form Designer generated code
90 override protected void OnInit(EventArgs e)
91 {
92 //
93 // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
94 //
95 InitializeComponent();
96 base.OnInit(e);
97 }
98
99 /// <summary>
100 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
101 /// 此方法的内容。
102 /// </summary>
103 private void InitializeComponent()
104 {
105 this.MyDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler( this.OnPageIndexChanged);
106 this.btnFirst.Click += new System.EventHandler( this.PagerButtonClick);
107 this.btnPrev.Click += new System.EventHandler( this.PagerButtonClick);
108 this.btnNext.Click += new System.EventHandler( this.PagerButtonClick);
109 this.btnLast.Click += new System.EventHandler( this.PagerButtonClick);
110 this.Load += new System.EventHandler( this.Page_Load);
111
112 }
113 #endregion
114
115 private void OnPageIndexChanged( object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
116 {
117 int startIndex ;
118 startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
119 MyDataGrid.CurrentPageIndex = e.NewPageIndex;
120 BindGrid();
121 ShowStats();
122 }
123 }
124 }
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Data.OleDb;
6 using System.Drawing;
7 using System.Web;
8 using System.Web.SessionState;
9 using System.Web.UI;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.HtmlControls;
12
13 namespace eMeng.Exam.DataGridPaging2
14 {
15 /// <summary>
16 /// DataGridPaging 的摘要说明。
17 /// </summary>
18 public class DataGridPaging2 : System.Web.UI.Page
19 {
20 protected System.Web.UI.WebControls.DataGrid MyDataGrid;
21 protected System.Web.UI.WebControls.Label lblPageCount;
22 protected System.Web.UI.WebControls.Label lblCurrentIndex;
23 protected System.Web.UI.WebControls.LinkButton btnFirst;
24 protected System.Web.UI.WebControls.LinkButton btnPrev;
25 protected System.Web.UI.WebControls.LinkButton btnNext;
26 protected System.Web.UI.WebControls.LinkButton btnLast;
27 private OleDbConnection cn = new OleDbConnection();
28
29 private void Page_Load( object sender, System.EventArgs e)
30 {
31 // 在此处放置用户代码以初始化页面
32 btnFirst.Text = " 最首页 ";
33 btnPrev.Text = " 前一页 ";
34 btnNext.Text = " 下一页 ";
35 btnLast.Text = " 最后页 ";
36 OpenDatabase();
37 BindGrid();
38 }
39 private void OpenDatabase()
40 {
41 cn.ConnectionString = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "
42 + HttpContext.Current.Server.MapPath( " http://www.cnblogs.com/aspxWeb.mdb.ascx ");
43 cn.Open();
44 }
45 private void ShowStats()
46 {
47 lblCurrentIndex.Text = " 第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页 ";
48 lblPageCount.Text = " 总共 " + MyDataGrid.PageCount.ToString() + " 页 ";
49 }
50
51 public void PagerButtonClick( object sender, EventArgs e)
52 {
53 string arg = ((LinkButton)sender).CommandArgument.ToString();
54 switch(arg)
55 {
56 case " next ":
57 if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
58 {
59 MyDataGrid.CurrentPageIndex += 1;
60 }
61 break;
62 case " prev ":
63 if (MyDataGrid.CurrentPageIndex > 0)
64 {
65 MyDataGrid.CurrentPageIndex -= 1;
66 }
67 break;
68 case " last ":
69 MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
70 break;
71 default:
72 MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
73 break;
74 }
75 BindGrid();
76 ShowStats();
77 }
78 public void BindGrid()
79 {
80 OleDbConnection myConnection = cn;
81 DataSet ds = new DataSet();
82 OleDbDataAdapter adapter = new OleDbDataAdapter( " Select Title,CreateDate from Document ", myConnection);
83 adapter.Fill(ds, " Document ");
84 MyDataGrid.DataSource = ds.Tables[ " Document "].DefaultView;
85 MyDataGrid.DataBind();
86 ShowStats();
87 }
88
89 #region Web Form Designer generated code
90 override protected void OnInit(EventArgs e)
91 {
92 //
93 // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
94 //
95 InitializeComponent();
96 base.OnInit(e);
97 }
98
99 /// <summary>
100 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
101 /// 此方法的内容。
102 /// </summary>
103 private void InitializeComponent()
104 {
105 this.MyDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler( this.OnPageIndexChanged);
106 this.btnFirst.Click += new System.EventHandler( this.PagerButtonClick);
107 this.btnPrev.Click += new System.EventHandler( this.PagerButtonClick);
108 this.btnNext.Click += new System.EventHandler( this.PagerButtonClick);
109 this.btnLast.Click += new System.EventHandler( this.PagerButtonClick);
110 this.Load += new System.EventHandler( this.Page_Load);
111
112 }
113 #endregion
114
115 private void OnPageIndexChanged( object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
116 {
117 int startIndex ;
118 startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
119 MyDataGrid.CurrentPageIndex = e.NewPageIndex;
120 BindGrid();
121 ShowStats();
122 }
123 }
124 }