1
<% @ Page Language="C#" %>
2
<% @ Import Namespace="System.Data" %>
3
<% @ Import Namespace="System.Data.OleDb" %>
4
<Script Language="C#" Runat="Server">
5
/*
6
Create By 飞刀
7
http://www.aspcn.com
8
2001-7-25 01:44
9![]()
10
Support .Net Framework Beta 2
11
*/
12
OleDbConnection MyConn;
13
int PageSize,RecordCount,PageCount,CurrentPage;
14
public void Page_Load(Object src,EventArgs e)
15
{
16
//设定PageSize
17
PageSize = 10;
18![]()
19
//连接语句
20
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
21
MyConn = new OleDbConnection(MyConnString);
22
MyConn.Open();
23![]()
24
//第一次请求执行
25
if(!Page.IsPostBack)
26
{
27
ListBind();
28
CurrentPage = 0;
29
ViewState["PageIndex"] = 0;
30![]()
31
//计算总共有多少记录
32
RecordCount = CalculateRecord();
33
lblRecordCount.Text = RecordCount.ToString();
34![]()
35
//计算总共有多少页
36
PageCount = RecordCount/PageSize;
37
lblPageCount.Text = PageCount.ToString();
38
ViewState["PageCount"] = PageCount;
39
}
40
}
41
//计算总共有多少条记录
42
public int CalculateRecord()
43
{
44
int intCount;
45
string strCount = "select count(*) as co from Score";
46
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
47
OleDbDataReader dr = MyComm.ExecuteReader();
48
if(dr.Read())
49
{
50
intCount = Int32.Parse(dr["co"].ToString());
51
}
52
else
53
{
54
intCount = 0;
55
}
56
dr.Close();
57
return intCount;
58
}
59![]()
60
ICollection CreateSource()
61
{
62![]()
63
int StartIndex;
64![]()
65
//设定导入的起终地址
66
StartIndex = CurrentPage*PageSize;
67
string strSel = "select * from Score";
68
DataSet ds = new DataSet();
69![]()
70
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
71
MyAdapter.Fill(ds,StartIndex,PageSize,"Score");
72![]()
73
return ds.Tables["Score"].DefaultView;
74
}
75
public void ListBind()
76
{
77
score.DataSource = CreateSource();
78
score.DataBind();
79![]()
80
lbnNextPage.Enabled = true;
81
lbnPrevPage.Enabled = true;
82
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
83
if(CurrentPage==0) lbnPrevPage.Enabled = false;
84
lblCurrentPage.Text = (CurrentPage+1).ToString();
85![]()
86
}
87![]()
88
public void Page_OnClick(Object sender,CommandEventArgs e)
89
{
90
CurrentPage = (int)ViewState["PageIndex"];
91
PageCount = (int)ViewState["PageCount"];
92![]()
93
string cmd = e.CommandName;
94
//判断cmd,以判定翻页方向
95
switch(cmd)
96
{
97
case "next":
98
if(CurrentPage<(PageCount-1)) CurrentPage++;
99
break;
100
case "prev":
101
if(CurrentPage>0) CurrentPage--;
102
break;
103
}
104![]()
105
ViewState["PageIndex"] = CurrentPage;
106![]()
107
ListBind();
108![]()
109
}
110
</script>
111
<html>
112
<head>
113
<title></title>
114
</head>
115
<body>
116
<form runat="server">
117
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
118
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页
119![]()
120
<asp:DataList id="score" runat="server"
121
HeaderStyle-BackColor="#aaaadd"
122
AlternatingItemStyle-BackColor="Gainsboro"
123
EditItemStyle-BackColor="yellow"
124
>
125
<ItemTemplate>
126
姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %>
127
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
128
</ItemTemplate>
129
</asp:DataList>
130
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
131
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
132![]()
133
</form>
134
</body>
135
</html>
136![]()
<% @ Page Language="C#" %> 2
<% @ Import Namespace="System.Data" %> 3
<% @ Import Namespace="System.Data.OleDb" %> 4
<Script Language="C#" Runat="Server"> 5
/* 6
Create By 飞刀 7
http://www.aspcn.com 8
2001-7-25 01:44 9

10
Support .Net Framework Beta 2 11
*/ 12
OleDbConnection MyConn; 13
int PageSize,RecordCount,PageCount,CurrentPage; 14
public void Page_Load(Object src,EventArgs e) 15
{ 16
//设定PageSize 17
PageSize = 10; 18

19
//连接语句 20
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;"; 21
MyConn = new OleDbConnection(MyConnString); 22
MyConn.Open(); 23

24
//第一次请求执行 25
if(!Page.IsPostBack) 26
{ 27
ListBind(); 28
CurrentPage = 0; 29
ViewState["PageIndex"] = 0; 30

31
//计算总共有多少记录 32
RecordCount = CalculateRecord(); 33
lblRecordCount.Text = RecordCount.ToString(); 34

35
//计算总共有多少页 36
PageCount = RecordCount/PageSize; 37
lblPageCount.Text = PageCount.ToString(); 38
ViewState["PageCount"] = PageCount; 39
} 40
} 41
//计算总共有多少条记录 42
public int CalculateRecord() 43
{ 44
int intCount; 45
string strCount = "select count(*) as co from Score"; 46
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn); 47
OleDbDataReader dr = MyComm.ExecuteReader(); 48
if(dr.Read()) 49
{ 50
intCount = Int32.Parse(dr["co"].ToString()); 51
} 52
else 53
{ 54
intCount = 0; 55
} 56
dr.Close(); 57
return intCount; 58
} 59

60
ICollection CreateSource() 61
{ 62

63
int StartIndex; 64

65
//设定导入的起终地址 66
StartIndex = CurrentPage*PageSize; 67
string strSel = "select * from Score"; 68
DataSet ds = new DataSet(); 69

70
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn); 71
MyAdapter.Fill(ds,StartIndex,PageSize,"Score"); 72

73
return ds.Tables["Score"].DefaultView; 74
} 75
public void ListBind() 76
{ 77
score.DataSource = CreateSource(); 78
score.DataBind(); 79

80
lbnNextPage.Enabled = true; 81
lbnPrevPage.Enabled = true; 82
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false; 83
if(CurrentPage==0) lbnPrevPage.Enabled = false; 84
lblCurrentPage.Text = (CurrentPage+1).ToString(); 85

86
} 87

88
public void Page_OnClick(Object sender,CommandEventArgs e) 89
{ 90
CurrentPage = (int)ViewState["PageIndex"]; 91
PageCount = (int)ViewState["PageCount"]; 92

93
string cmd = e.CommandName; 94
//判断cmd,以判定翻页方向 95
switch(cmd) 96
{ 97
case "next": 98
if(CurrentPage<(PageCount-1)) CurrentPage++; 99
break; 100
case "prev": 101
if(CurrentPage>0) CurrentPage--; 102
break; 103
} 104

105
ViewState["PageIndex"] = CurrentPage; 106

107
ListBind(); 108

109
} 110
</script> 111
<html> 112
<head> 113
<title></title> 114
</head> 115
<body> 116
<form runat="server"> 117
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录 118
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 119

120
<asp:DataList id="score" runat="server" 121
HeaderStyle-BackColor="#aaaadd" 122
AlternatingItemStyle-BackColor="Gainsboro" 123
EditItemStyle-BackColor="yellow" 124
> 125
<ItemTemplate> 126
姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %> 127
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" /> 128
</ItemTemplate> 129
</asp:DataList> 130
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" /> 131
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" /> 132

133
</form> 134
</body> 135
</html>136




浙公网安备 33010602011771号