Simple and Easy-to-use ASP. NET paging class (supports AJAX and custom text)

Source: Internet
Author: User

Simple and Easy-to-use ASP. NET paging class (supports AJAX and custom text)

This article mainly introduces the simple and easy-to-use ASP. NET paging class (supporting AJAX and user-defined text). This article provides the implementation code and usage methods. For more information, see

It is useful when JavaScript UI controls are not used for websites

Usage:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Var ps = new PageString ();

 

/* Optional parameter */

 

Ps. SetIsEnglish = true; // whether it is English (default: false)

Ps. SetIsShowText = true; // whether to display paging text (default: true)

// Ps. textFormat = "" (default value: "span class = \" pagetext \ "strong" Total "/strong": {0} "strong" current "/strong": {1}/{2}/span)

// Ps. SetPageIndexName Request ["pageIndex"] (default value: "pageIndex ")

Ps. SetIsAjax = false; // (default value: "false ")

 

/* Function parameters */

Int total = 10000;

Int pageSize = 10;

Int pageIndex = Convert. ToInt32 (Request ["pageIndex"]);

 

Var page = ps. ToString (total, pageSize, pageIndex, "/UI/PageStringTest. aspx? ");

 

// Obtain page html output

Response. Write (page );

Effect:

Code:

?

1

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

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

Using System. Text. RegularExpressions;

 

Namespace SyntacticSugar

{

/// <Summary>

/// ** Description: Paging class

/// ** Founding Date:

/// ** Modification time :-

/// ** Prepared by sunkaixuan

Public class PageString

{

/// <Summary>

/// Whether it is in English (default: false)

/// </Summary>

Public bool SetIsEnglish {get; set ;}

/// <Summary>

/// Whether to display paging text (default: true)

/// </Summary>

Public bool SetIsShowText {get; set ;}

/// <Summary>

/// Style (default: "pagination ")

/// </Summary>

Public string SetClassName {get; set ;}

/// <Summary>

/// Paging parameter name (default: "pageIndex ")

/// </Summary>

Public string SetPageIndexName {get; set ;}

/// <Summary>

/// Whether asynchronous synchronization href = ''asynchronous onclick = ajaxPage () (default: false)

/// </Summary>

Public bool SetIsAjax {get; set ;}

 

/// <Summary>

/// Custom text

// String. Format ("{0} {1} {2}", "total records", "Current page number", "total page number ")

// Default value: span class = \ "pagetext \" strong, total/strong: {0} strong, current/strong: {1}/{2}/span

/// </Summary>

Public string SetTextFormat {get; set ;}

 

Public PageString ()

{

SetIsEnglish = false;

SetIsShowText = true;

SetTextFormat = "<span class = \" pagetext \ "> <strong> total </strong>: {0} <strong> current </strong>: {1}/{2} </span> ";

SetClassName = "pagination ";

SetPageIndexName = "pageIndex ";

SetIsAjax = false;

}

 

/* Free Style

. Pagination. click {cursor: pointer}

. Pagination a {text-decoration: none; border: 1px solid # AAE; color: # 15B; font-size: 13px; border-radius: 2px ;}

. Pagination span {color: #666; font-size: 13px; display: inline-block; border: 1px solid # ccc; padding: 0.2em 0.6em ;}

. Pagination span. pagetext {border: none}

. Pagination a: hover {background: # 26B; color: # fff ;}

. Pagination a {display: inline-block; padding: 0.2em 0.6em ;}

. Pagination. page_current {background: # 26B; color: # fff; border: 1px solid # AAE; margin-right: 5px ;}

. Pagination {margin-top: 20px ;}

. Pagination. current. prev,. pagination. current. next {color: #999; border-color: #999; background: # fff ;}

**/

 

/// <Summary>

/// Paging algorithm <1> 20 pages home page Previous Page 1 2 3 4 5 6 7 8 9 10 next page last page

/// </Summary>

/// <Param name = "total"> total number of records </param>

/// <Param name = "pageSize"> Number of records per page </param>

/// <Param name = "pageIndex"> current page </param>

/// <Param name = "query_string"> Url parameter </param>

/// <Returns> </returns>

Public string ToString (int total, int pageSize, int pageIndex, string query_string)

{

 

Int allpage = 0;

Int next = 0;

Int pre = 0;

Int startcount = 0;

Int endcount = 0;

StringBuilder pagestr = new StringBuilder ();

PageIndex = 0? 1: pageIndex;

Pagestr. AppendFormat ("<div class = \" {0} \ ">", SetClassName );

If (pageIndex <1) {pageIndex = 1 ;}

// Calculate the total number of pages

If (pageSize! = 0)

{

Allpage = (total/pageSize );

Allpage = (total % pageSize )! = 0? Allpage + 1: allpage );

Allpage = (allpage = 0? 1: allpage );

}

Next = pageIndex + 1;

Pre = pageIndex-1;

Startcount = (pageIndex + 5)> allpage? Allpage-9: pageIndex-4; // start Number of the intermediate page

// End number of the intermediate page

Endcount = pageIndex <5? 10: pageIndex + 5;

If (startcount <1) {startcount = 1;} // to avoid negative output, if it is smaller than 1, it starts from number 1.

If (allpage <endcount) {endcount = allpage;} // the possibility of page number + 5 will generate the final output sequence number greater than the total page number, then it should be controlled within the number of pages

 

Bool isFirst = pageIndex = 1;

Bool isLast = pageIndex = allpage;

 

If (SetIsShowText)

Pagestr. AppendFormat (SetTextFormat, total, pageIndex, allpage );

 

If (isFirst)

{

Pagestr. Append ("<span> homepage </span> <span> previous page </span> ");

}

Else

{

Pagestr. appendFormat ("<a href = \" {0} pageIndex = 1 \ "> homepage </a> <a href = \" {0} pageIndex = {1} \ "> previous Page </a> ", query_string, pre );

}

// Intermediate page processing, which increases the time complexity and reduces the space complexity

For (int I = startcount; I <= endcount; I ++)

{

Bool isCurent = pageIndex = I;

If (isCurent)

{

Pagestr. Append ("<a class = \" page_current \ ">" + I + "</a> ");

}

Else

{

Pagestr. Append ("<a href = \" "+ query_string +" pageIndex = "+ I +" \ ">" + I + "</a> ");

}

 

}

If (isLast)

{

Pagestr. Append ("<span> next page </span> <span> last page </span> ");

}

Else

{

Pagestr. append ("<a href = \" "+ query_string +" pageIndex = "+ next +" \ "> next page </a> <a href = \" "+ query_string +" pageIndex = "+ allpage +" \ "> last page </a> ");

}

Pagestr. AppendFormat ("</div> ");

Return ConversionData (pagestr. ToString ());

}

 

Private string ConversionData (string page)

{

If (SetIsEnglish)

{

Page = page. replace ("Previous Page", "Previous "). replace ("Next", "Next "). replace ("total", "total "). replace ("Current", "Current "). replace ("entry", "records "). replace ("Homepage", "First "). replace ("Last page", "Last ");

}

If (SetIsAjax)

{

Var matches = Regex. Matches (page, @ "href \ = "".*? ", RegexOptions. Singleline );

If (matches! = Null & matches. Count> 0)

{

Foreach (Match m in matches)

{

Page = page. replace (m. value, string. format ("class = \" click \ "onclick = \" ajaxPage ('{0}') \ "", Regex. match (m. value, string. format (@ "{0 }\= (\ d +)", SetPageIndexName )). groups [1]. value ));

}

}

}

Return page;

 

}

 

}

 

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.