1
[ToolboxData("<{0}:Paging runat=server></{0}:Paging>")]
2
public class Paging : WebControl
3
{
4
public int Records
5
{
6
get
7
{
8
object o = this.ViewState["Records"];
9
if (o == null)
10
{
11
return 1;
12
}
13
else
14
{
15
return (int)o;
16
}
17
}
18
set
19
{
20
this.ViewState["Records"] = value;
21
}
22
}
23
24
public int PageSize
25
{
26
get
27
{
28
object o = this.ViewState["PageSize"];
29
if (o == null)
30
{
31
return 1;
32
}
33
else
34
{
35
return (int)o;
36
}
37
}
38
set
39
{
40
41
this.ViewState["PageSize"] = value;
42
}
43
}
44
45
46
public int ToPage
47
{
48
get
49
{
50
if (DesignMode)
51
{
52
return 1;
53
}
54
string s = Page.Request[PageName];
55
if (s == null)
56
{
57
return 1;
58
}
59
else
60
{
61
return int.Parse(s);
62
}
63
}
64
}
65
66
public int PageCount;
67
68
public string PageName = "Topage";
69
70
71
protected override void RenderContents(HtmlTextWriter output)
72
{
73
Build(output,CalculatePageCount);
74
}
75
76
private int CalculatePageCount(int records,int pageSize)
77
{
78
int mod = records % pageSize;
79
if (mod > 0)
80
{
81
return (records / pageSize + 1);
82
}
83
else
84
{
85
return (records / pageSize);
86
}
87
}
88
89
public delegate int CalculatePageCountHandel(int records, int pageSize);
90
91
private void Build(HtmlTextWriter output,CalculatePageCountHandel kk)
92
{
93
PageCount = kk(Records,PageSize);
94
string prev = "";
95
string next = "";
96
97
if (ToPage == 1)
98
{
99
prev = "prev ";
100
}
101
else
102
{
103
prev = string.Format("<a href=\"
{0}\">prev</a> ", GetUrl(ToPage - 1));
104
}
105
if (ToPage >= PageCount)
106
{
107
next = "next";
108
}
109
else
110
{
111
next = string.Format("<a href=\"
{0}\">next</a> ", GetUrl(ToPage + 1));
112
}
113
string temp = "";
114
int previousPagesCount = 3;
115
for (int i = ToPage - previousPagesCount; i < ToPage; i++)
116
{
117
if (i > 0)
118
{
119
temp += string.Format("<a href=\"
{0}\">{1}</a> ", GetUrl(i), i);
120
}
121
}
122
temp += string.Format("<a href=\"
{0}\" class=\"selected\">{1}</a> ", GetUrl(ToPage), ToPage);
123
int nextPagesCount = 3;
124
for (int i = ToPage + 1; i <= PageCount && i <= ToPage + nextPagesCount; i++)
125
{
126
127
temp += string.Format("<a href=\"
{0}\">{1}</a> ", GetUrl(i), i);
128
}
129
if (ToPage + 1 + 4 < PageCount)
130
{
131
temp += "
";
132
}
133
temp = "<span>" +prev + temp + next+"</span>";
134
135
136
output.Write(temp);
137
}
138
public NameValueCollection ExtendProperty;
139
private string GetUrl(int number)
140
{
141
if (this.DesignMode)
142
{
143
return string.Empty;
144
}
145
NameValueCollection list = new NameValueCollection(Page.Request.QueryString);
146
if (list[PageName] == null)
147
{
148
list.Add(PageName, number.ToString());
149
}
150
else
151
{
152
list[PageName] = number.ToString();
153
}
154
string key;
155
if (ExtendProperty != null)
156
{
157
for (int i = 0; i < ExtendProperty.Count; i++)
158
{
159
key = ExtendProperty.Keys[i];
160
if (list[key] == null)
161
{
162
list.Add(key, ExtendProperty[key]);
163
}
164
else
165
{
166
list[key] = ExtendProperty[key];
167
}
168
}
169
}
170
string url = Page.Request.Path+"?";
171
for (int i = 0; i < list.Count; i++)
172
{
173
url += list.Keys[i] + "=" +HttpUtility.UrlEncode(list[i]);
174
if (i != list.Count - 1)
175
{
176
url += "&";
177
}
178
}
179
return url;
180
181
}
182
}

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

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



117

118



119



120

121

122



123

124

125



126

127



128

129

130



131


132

133

134

135

136

137

138

139

140



141

142



143

144

145

146

147



148

149

150

151



152

153

154

155

156



157

158



159

160

161



162

163

164

165



166

167

168

169

170

171

172



173

174

175



176

177

178

179

180

181

182

pageSize control


























































































































































On Make url function , You can replace some code