
下面这个实例中,我们将可以根据DataGrid分页机制,通过编程做一个十分有个性、功能十分完善的分页效果。
在DataCon Web项目里新建一个Web 窗体,命名为DataGrid_Sample3.aspx,并添加一个DataGrid控件,四个LinkButton控件,一个DropDownList控件,一个Label1控件,一个TextBox控件,一个Button控件,一个正规则表达式验证控件。
DataGrid_Sample3.aspx的主要HTML代码如下:
1
<form id="Form1" method="post" runat="server">
2
<FONT face="宋体">
3
<asp:DataGrid
4
id="DataGrid1"
5
runat="server" Width="368px" Height="144px" AllowPaging="True" AllowCustomPaging="True"
6
PageSize="999">
7
<ItemStyle Font-Size="X-Small"></ItemStyle>
8
<HeaderStyle BackColor="SkyBlue"></HeaderStyle>
9
<PagerStyle Visible="False" PageButtonCount="5" Mode="NumericPages"></PagerStyle>
10
</asp:DataGrid><br>
11
<asp:LinkButton
12
id="LinkButton1" runat="server"
13
Font-Size="X-Small">第一页</asp:LinkButton>
14
<asp:LinkButton
15
id="LinkButton2" runat="server"
16
Font-Size="X-Small">上一页</asp:LinkButton>
17
<asp:LinkButton
18
id="LinkButton3" runat="server"
19
Font-Size="X-Small">下一页</asp:LinkButton>
20
<asp:LinkButton
21
id="LinkButton4" runat="server"
22
Font-Size="X-Small">最后页</asp:LinkButton>
23
<asp:Label
24
id="Label1" runat="server" Width="168px"
25
Font-Size="X-Small">Label</asp:Label>
26
<br>
27
每页记录条数
28
<asp:DropDownList
29
id="DropDownList1" runat="server"
30
Height="32px" Width="48px" AutoPostBack="True">
31
<asp:ListItem value="3">3</asp:ListItem>
32
<asp:ListItem value="5">5</asp:ListItem>
33
<asp:ListItem value="7" Selected="True">7</asp:ListItem>
34
<asp:ListItem value="9">9</asp:ListItem>
35
<asp:ListItem value="12">12</asp:ListItem>
36
<asp:ListItem value="15">15</asp:ListItem>
37
</asp:DropDownList>
38
跳转至
39
<asp:TextBox
40
id="TextBox1" runat="server" Width="48px"></asp:TextBox>
41
<asp:Button id="Button1" runat="server"
42
Width="40px" Text="->Go"></asp:Button>
43
<asp:RegularExpressionValidator
44
id="RegularExpressionValidator1" runat="server"
45
Font-Size="XX-Small" ErrorMessage="请输入正确的页码"
46
ValidationExpression="[1-9]{1}\d{0,}"
47
ControlToValidate="TextBox1" >
48
</asp:RegularExpressionValidator>
49
</FONT>
50
</form>
51
52
http://www.sucsky.com/info/8120.htm
53

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

DataGrid_Sample3.aspx.vb的代码如下:

'----code begin----
1
'引入名称空间
2
Imports System
3
Imports System.Data
4
Imports System.Web.UI
5
Public Class DataGrid_Sample3_aspx
6
Inherits System.Web.UI.Page
7
Web 窗体设计器生成的代码
10
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
11
'在此处放置初始化页的用户代码
12
If Not IsPostBack Then
13
viewstate("startpage") = 0
14
'利用viewstate("startpage")来保存DataGrid1当前页码索引
15
'初始化为0,即第一页
16
viewstate("PageSize") = 7
17
'viewstate("PageSize")来保存PageSize,初始化为7
18
End If
19
getdata() '条用分页过程
20
End Sub
21
'编写读取数据通用过程,利用该过程读出数据,根据指定数据进行分页,
22
'并绑定到DataGrid控件上
23
Sub getdata()
24
Dim connstr As String '声明数据库连接字符
25
Dim mycon As OleDb.OleDbConnection
26
'因为使用Aeecss数据库,所以声明OleDConnention对象
27
Dim mycmd As OleDb.OleDbDataAdapter
28
'声明DataAdapter对象
29
Dim mysql As String
30
'声明Command命令的 SQL字符串
31
Try
32
connstr = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(".") + "\StudentInfor.mdb"
33
'为连接字符串赋值
34
mycon = New OleDb.OleDbConnection(connstr)
35
'实例化Connection对象
36
mysql = "Select id,name,sex,class,tel ,email from student"
37
'设置SQL语句,即查询数据库中所有内容
38
mycmd = New OleDb.OleDbDataAdapter(mysql, mycon)
39
Dim dt As Data.DataSet = New Data.DataSet
40
'声明DataSet对象,并实例话
41
mycmd.Fill(dt, viewstate("startpage") * viewstate("PageSize"), viewstate("PageSize"), "infor")
42
'填充数据,即在内存中生成DataSet模型数据库
43
'利用DataAdapter.Fill的重载功能, viewstate("startpage") * viewstate("PageSize")乘机结果是第几条记录
44
DataGrid1.DataSource = dt.Tables("infor")
45
'为DataGrid1控件指定数据源
46
DataGrid1.DataBind()
47
'执行绑定
48
Catch ex As Exception
49
Response.Write("程序出错,信息描述如下:<br>" & ex.Message)
50
Finally
51
mycon.Close()
52
End Try
53
getpages()
54
End Sub
55
'获取记录分页信息
56
Sub getpages()
57
Dim mycon As OleDb.OleDbConnection = New OleDb.OleDbConnection(" provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath(".") & "\StudentInfor.mdb")
58
mycon.Open()
59
Try
60
Dim mycmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("select count(*) from student", mycon)
61
viewstate("pc") = mycmd.ExecuteScalar \ viewstate("PageSize") + 1
62
Label1.Text = "当前为第[" & (viewstate("startpage") + 1).ToString & "]页/共[" & (viewstate("pc")) & "]页"
63
Catch ex As Exception
64
Response.Write("程序出错,信息描述如下:<br>" & ex.Message)
65
Finally
66
mycon.Close()
67
End Try
68
End Sub
69
'跳转至第一页
70
Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
71
If viewstate("startpage") <> 0 Then
72
'如果当前页面不是第一页则执行
73
viewstate("startpage") = 0
74
End If
75
getdata()
76
End Sub
77
'跳转至上一页 ,即页码减1
78
Private Sub LinkButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
79
If viewstate("startpage") > 0 Then
80
'如果当前页码索引大于0则执行
81
viewstate("startpage") = viewstate("startpage") - 1
82
End If
83
getdata()
84
End Sub
85
'跳转至下一页 ,即页码加1
86
Private Sub LinkButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton3.Click
87
If viewstate("startpage") < viewstate("pc") - 1 Then
88
'如果当前页码索引小于(总页数-1)时 则执行
89
viewstate("startpage") = viewstate("startpage") + 1
90
End If
91
getdata()
92
End Sub
93
' 跳转至最后一页
94
Private Sub LinkButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton4.Click
95
If viewstate("startpage") <> viewstate("pc") - 1 Then
96
'如果当前页面不是最后一页则执行
97
viewstate("startpage") = viewstate("pc") - 1
98
End If
99
getdata()
100
End Sub
101
'跳转至选中页码
102
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
103
viewstate("PageSize") = DropDownList1.Selectedvalue
104
viewstate("startpage") = 0
105
getdata()
106
End Sub
107
'跳转至手工输入的代码页码
108
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
109
If Int(TextBox1.Text) <= viewstate("pc") Then
110
viewstate("startpage") = Int(TextBox1.Text) - 1
111
getdata()
112
End If
113
End Sub
114
End Class
115
'-----code end ----------
116

2

3

4

5

6

7

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116
