There are many paging controls in Asp.net webform, such as aspnetpager. In MVC development mode, these controls are not suitable, so I wrote one myself.
In the first version, refresh the htmlhelper version:
The Extension Method in C #3.0 is used here.
Public static class cutepage
{
/// <Summary>
/// Pager display
/// </Summary>
/// <Param name = "html"> </param>
/// <Param name = "currentpagestr"> querystringkey that identifies the current page number </param>
/// <Param name = "pagesize"> display on each page </param>
/// <Param name = "totalcount"> total data volume </param>
/// <Returns> </returns>
Public static string Pager (this htmlhelper HTML, string currentpagestr, int pagesize, int totalcount)
{
VaR querystring = html. viewcontext. httpcontext. Request. querystring;
Int currentpage = 1; // current page
VaR totalpages = math. Max (totalcount + pagesize-1)/pagesize, 1); // total number of pages
VaR dict = new system. Web. Routing. routevaluedictionary (html. viewcontext. routedata. values );
VaR output = new system. Text. stringbuilder ();
If (! String. isnullorempty (querystring [currentpagestr])
{
// Bind to the corresponding querystring
Foreach (string key in querystring. Keys)
If (querystring [Key]! = NULL &&! String. isnullorempty (key ))
Dict [Key] = querystring [Key];
Int. tryparse (querystring [currentpagestr], out currentpage );
}
Else
{
// If this is the first time you access this page without the currentpagestr parameter, add currentpagestr = 1
If (! Dict. containskey (currentpagestr ))
{
Dict. Add (currentpagestr, 1 );
}
Int. tryparse (dict [currentpagestr]. tostring (), Out currentpage );
}
If (currentpage <= 0) currentpage = 1;
If (totalpages> 1)
{
If (currentpage! = 1)
{
// Process the homepage connection
Dict [currentpagestr] = 1;
Output. appendformat ("{0}", HTML. routelink ("Homepage", dict ));
}
If (currentpage> 1)
{
// Process the connection of the previous page
Dict [currentpagestr] = currentpage-1;
Output. append (html. routelink ("Previous Page", dict ));
}
Else
{
Output. append ("Previous Page ");
}
Output. append ("");
Int currint = 5;
For (INT I = 0; I <= 10; I ++)
{
// A total of 10 page numbers can be displayed, including the first 5 and the last 5
If (currentpage + I-currint)> = 1 & (currentpage + I-currint) <= totalpages)
If (currint = I)
{
// Process the current page
Output. append (string. Format ("{0}", currentpage ));
}
Else
{
// General page processing
Dict [currentpagestr] = currentpage + I-currint;
Output. append (html. routelink (currentpage + I-currint). tostring (), dict ));
}
Output. append ("");
}
If (currentpage <totalpages)
{
// Process the next page
Dict [currentpagestr] = currentpage + 1;
Output. append (html. routelink ("Next", dict ));
}
Else
{
Output. append ("next page ");
}
Output. append ("");
If (currentpage! = Totalpages)
{
Dict [currentpagestr] = totalpages;
Output. append (html. routelink ("last page", dict ));
}
Output. append ("");
}
Output. appendformat ("{0}/{1}", currentpage, totalpages); // This statistic can be added without adding
Return output. tostring ();
}
}
Call method on the page <% = html. Pager ("pageindex", 10, itemscout) %>
Of course, I will introduce the pagination control of the new jquery version.:
/* Generate paging navigation
Pageindex -- index of the current page
Pagesize -- number of rows per page
Totalcount -- total number of rows
Ajaxmethod -- js method written by jquery for refreshing data retrieval
Target --- page elements output by PAGE, such as <div>
*/
Function initepagenav (pageindex, pagesize, totalcount, ajaxmethod, target ){
VaR currentpage = pageindex + 1; // the current page
VaR totalpages = parseint (totalcount + pagesize-1)/pagesize); // the total number of pages
VaR output = "";
Output + = "nth" + (currentpage-1) * pagesize + 1) + "-" + currentpage * pagesize + "bar" + "Total" + totalcount + "bar ";
Output + = "";
If (currentpage <= 0) currentpage = 1;
If (totalpages> 1)
{
If (currentpage! = 1)
{
// Process the homepage connection
Output + = "<a href = \" javascript: "+ ajaxmethod +" (0) \ "> homepage </a> ";
}
Output + = "";
If (currentpage> 1)
{
// Process the connection of the previous page
Output + = "<a href = \" javascript: "+ ajaxmethod +" ("+ (currentpage-2) +") \ "> previous page </a> ";
}
Else
{
Output + = "Previous Page ";
}
Output + = "| ";
VaR currint = 5;
For (VAR I = 0; I <= 10; I ++)
{
// A total of 10 page numbers can be displayed, including the first 5 and the last 5
If (currentpage + I-currint)> = 1 & (currentpage + I-currint) <= totalpages)
If (currint = I)
{
// Process the current page
Output + = currentpage;
Output + = "";
}
Else
{
// General page processing
Output + = "<a href = \" javascript: "+ ajaxmethod +" ("+ (currentpage + I-currint-1) + ") \ ">" + (currentpage + I-currint) + "</a> ";
Output + = "";
}
}
If (currentpage <totalpages)
{
// Process the next page
Output + = "| <a href = \" javascript: "+ ajaxmethod +" ("+ (currentpage) +") \ "> next page </a> ";
}
Else
{
Output + = "| next page ";
}
Output + = "";
If (currentpage! = Totalpages)
{
Output + = "<a href = \" javascript: "+ ajaxmethod +" ("+ (totalPages-1) +") \ "> last page </a> ";
}
Output + = "</span> ";
}
If (totalcount> 0)
{
$ ("#" + Target). Text ("");
$ ("#" + Target). append (output );
}
}
This function is used in combination with the method of refreshing data as follows:
<Script language = "JavaScript">
$ (). Ready (function (){
// Get comments during page Initialization
Getcomments(0 );
});
// Get comments
Function getcomments (pageindex ){
$. Post ("http://www.cnblogs.com/Services/GetComments.ashx", {mid: <% = model. Mid %>, pageindex: pageindex, pagesize: 10 },
Function (data, textstatus ){
$ ("# Videobodycommentlist"). Text (""); // clear the page data content first
$ ("# Videobodycommentlist"). append (data. Result); // fill in the obtained data
}, "JSON ");
}
</SCRIPT>
Http://www.cnblogs.com/wangergo/archive/2009/06/20/1507366.html