Datalist in asp.net USES the implementation method of digital paging

  • 2020-05-12 02:26:55
  • OfStack

 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test(Datalist Digital paging ).aspx.cs" Inherits="Test_Datalist Digital paging _" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title> No title page </title> 
<link href="CSS/CSS.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<asp:DataList ID="DataList1" runat="server"> 
<ItemTemplate> 
<asp:Label ID="Label1" runat="server" Text='<%# Eval("mid") %>'></asp:Label> 
</ItemTemplate> 
</asp:DataList> 
</div> 
<br /> 
<div id="PageInfo" runat="server" class="LPageBar"></div> 
</form> 
</body> 
</html> 

CS code:
 
using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
public partial class Test_Datalist Digital paging _ : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
//ComFunction cf = new ComFunction(); 
//DataSet ds = cf.DataBind("M_dizhi"); 
//this.PageInfo.InnerHtml = PageNums.GetPageNum(ds, DataList1, 12); 
this.PageInfo.InnerHtml = PageNums.GetPageSql("M_dizhi", DataList1, 12); 
} 
} 

PageNums.cs
 
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Data.SqlClient; 
/// <summary> 
///PageNums  Summary of  
/// </summary> 
public class PageNums 
{ 
/// </summary> 
/// <param name="ds">DataSet The instance </param> 
/// <param name="datalistname">DataList The name of the </param> 
/// <param name="pagesize"> Page size </param> 
public static string GetPageNum(DataSet ds, DataList datalistname, int pagesize) 
{ 
PagedDataSource objPds = new PagedDataSource(); 
objPds.DataSource = ds.Tables[0].DefaultView; 
objPds.AllowPaging = true; 
int total = ds.Tables[0].Rows.Count; 
objPds.PageSize = pagesize; 
int page; 
if (HttpContext.Current.Request.QueryString["page"] != null) 
page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]); 
else 
page = 1; 
objPds.CurrentPageIndex = page - 1; 
datalistname.DataSource = objPds; 
datalistname.DataBind(); 
int allpage = 0; 
int next = 0; 
int pre = 0; 
int startcount = 0; 
int endcount = 0; 
string pagestr = ""; 
if (page < 1) { page = 1; } 
// Total page count  
if (pagesize != 0) 
{ 
allpage = (total / pagesize);// Total page count  
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage); 
allpage = (allpage == 0 ? 1 : allpage); 
} 
next = page + 1; 
pre = page - 1; 
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;// Start of middle page  
// Middle page termination sequence number  
endcount = page < 5 ? 10 : page + 5; 
if (startcount < 1) { startcount = 1; } // To avoid negative Numbers in the output, set if less than 1 From the serial number 1 start  
if (allpage < endcount) { endcount = allpage; } // The page number +5 The possibility that the final output sequence number will be greater than the total page number will be controlled within the page number  
pagestr = " A total of " + allpage + " page "; 
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\"> Home page </a>;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\"> on 1 page </a>" : " Home page   on 1 page "; 
// Middle page processing, which increases the time complexity and decreases the space complexity  
for (int i = startcount; i <= endcount; i++) 
{ 
pagestr += page == i ? "<font color=\"#ff0000\">" + i + "</font>" : "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + i + "\">" + i + "</a>"; 
} 
pagestr += page != allpage ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + next + "\"> Under the 1 page </a><a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\"> At the end of the page </a>" : "  Under the 1 page   At the end of the page "; 
return pagestr; 
} 

public static string GetPageSql(string FileName, DataList datalistname, int pagesize) 
{ 
int page; 
int allpage = 0; 
int next = 0; 
int pre = 0; 
int startcount = 0; 
int endcount = 0; 
string pagestr = ""; 
if (HttpContext.Current.Request.QueryString["page"] != null) 
page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]); 
else 
page = 1; 
if (page < 1) { page = 1; } 
DataSet ds = DataBindSql("EXEC pagesql '*',' FROM " + FileName + " '," + pagesize + "," + page, FileName); 
datalistname.DataSource = ds; 
datalistname.DataBind(); 
int total = DataBind(FileName); 
// Total page count  
if (pagesize != 0) 
{ 
allpage = (total / pagesize);// Total page count  
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage); 
allpage = (allpage == 0 ? 1 : allpage); 
} 
next = page + 1; 
pre = page - 1; 
startcount = page / 5 * 5;// Start of middle page  
// Middle page termination sequence number  
endcount = startcount+5; 
if (startcount < 1) { startcount = 1; } // To avoid negative Numbers in the output, set if less than 1 From the serial number 1 start  
if (allpage < endcount) { endcount = allpage; } // The page number +5 The possibility that the final output sequence number will be greater than the total page number will be controlled within the page number  
pagestr = "<a>"+page + "/" + allpage + " page </a>"; 
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\"> Home page </a><a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\"> on 1 page </a>" : " Home page   on 1 page "; 
// Middle page processing, which increases the time complexity and decreases the space complexity  
for (int i = startcount; i <= endcount; i++) 
{ 
pagestr += page == i ? "<font color=\"#ff0000\">" + i + "</font>" : "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + i + "\">" + i + "</a>"; 
} 
pagestr += page != allpage ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + next + "\"> Under the 1 page </a><a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\"> At the end of the page </a>" : "  Under the 1 page   At the end of the page "; 
return pagestr; 
} 
private static int DataBind(string FileName) 
{ 
string sql = "select * from " + FileName; 
DbConnection dc = new DbConnection(); 
SqlConnection mycon = new SqlConnection(dc.ConnectionString); 
SqlDataAdapter mypter = new SqlDataAdapter(sql, mycon); 
DataSet ds = new DataSet(); 
mycon.Open(); 
mypter.Fill(ds, FileName); 
mycon.Close(); 
int total = ds.Tables[0].Rows.Count; 
return total; 
} 
private static DataSet DataBindSql(string sql, string FileName) 
{ 
DbConnection dc = new DbConnection(); 
SqlConnection mycon = new SqlConnection(dc.ConnectionString); 
SqlDataAdapter mypter = new SqlDataAdapter(sql,mycon); 
DataSet ds = new DataSet(); 
mycon.Open(); 
mypter.Fill(ds, FileName); 
mycon.Close(); 
return ds; 
} 
} 

Stored procedure pagesql
 
CREATE PROCEDURE pagesql 
@sqlSelect varchar(800) --SELECT  behind  FROM  In front of the   the   field   Don't have to include SELECT 
,@sqlFrom varchar(800) --FROM  behind   the   field   contains FROM 
,@countPerPage int --  Number of data rows per page  
,@toPage int -- Page number to go to  
AS 
BEGIN 

--  Based on the number of rows per page   and   Page number to go to   get   Data start and stop point  
Declare @start int 
Declare @end int 
set @end = @countPerPage * @toPage 
set @start = @countPerPage * (@toPage - 1) + 1 

--  Temporary table name   Random naming  
Declare @tmpTable varchar(10) 
SET @tmpTable ='#tmp' 
Declare @sqlStr varchar(800) 
--  Create the data source to the temporary table  
SELECT @sqlStr = 'SELECT Identity(int,1,1) AS RowIndex,' 
SELECT @sqlStr = @sqlStr + rtrim(@sqlSelect) + ' INTO '+ @tmpTable 
SELECT @sqlStr = @sqlStr + rtrim(@sqlFrom) 
--  Query temporary table   Get the data you need  
SELECT @sqlStr = @sqlStr + ' '+'SELECT '+ rtrim(@sqlSelect) +' FROM ' + @tmpTable 
SELECT @sqlStr = @sqlStr + ' WHERE RowIndex BETWEEN ' + Convert(char,@start) + " AND " + Convert(char,@end) 
--  Delete temporary table  
SELECT @sqlStr = @sqlStr + ' '+'DROP TABLE '+@tmpTable 
EXEC (@sqlStr) 

END 
GO 

Related articles: