The realization method of DataList using digital paging in asp.net

Source: Internet
Author: User
Tags copy count end final httpcontext sql net string
Copy CodeThe code is as follows:
<%@ 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 ">
<title> Untitled Page </title>
<link href= "Css/css.css" rel= "stylesheet" type= "Text/css"/>
<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>

CS Code:
Copy CodeThe code is as follows:
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 pagination _: 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
Copy CodeThe code is as follows:
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>
Summary description of Pagenums
</summary>
public class Pagenums
{
</summary>
<param name= "DS" >dataset instance </param>
<param name= "Datalistname" >datalist name </param>
<param name= "pagesize" > Paging 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;}
Calculate Total Pages
if (pagesize!= 0)
{
Allpage = (total/pagesize);//Total number of pages calculated
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;//Middle page starting serial number
Middle page Termination serial number
Endcount = Page < 5? 10:page + 5;
if (Startcount < 1) {startcount = 1;}//To avoid negative output, set if less than 1 starting from ordinal 1
if (Allpage < endcount) {endcount = allpage}//page +5 is likely to produce a final output ordinal greater than the total page number, then control it within the page count
PAGESTR = "Total" + allpage + "page";
Pagestr + = page > 1? "<a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath +"? page=1\ "> Home </a>;<a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath + "page=" + Pre + "> Prev </a>": "First prev";
Middle page processing, which increases the time complexity and reduces the complexity of the space
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 +" \ "> next page </a><a Href=\ "" + HttpContext.Current.Request.CurrentExecutionFilePath + "" page= "+ allpage +" \ "> Last </a>": "Next last 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);
Calculate Total Pages
if (pagesize!= 0)
{
Allpage = (total/pagesize);//Total number of pages calculated
Allpage = (total% pagesize)!= 0? allpage + 1:allpage);
Allpage = (Allpage = = 0 1:allpage);
}
Next = page + 1;
Pre = page-1;
Startcount = PAGE/5 * 5;//Center page starting serial number
Middle page Termination serial number
Endcount = startcount+5;
if (Startcount < 1) {startcount = 1;}//To avoid negative output, set if less than 1 starting from ordinal 1
if (Allpage < endcount) {endcount = allpage}//page +5 is likely to produce a final output ordinal greater than the total page number, then control it within the page count
Pagestr = "<a>" +page + "/" + allpage + "page </a>";
Pagestr + = page > 1? "<a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath +"? page=1\ "> Home </a><a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath + "page=" + Pre + "> Prev </a>": "First prev";
Middle page processing, which increases the time complexity and reduces the complexity of the space
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 +" \ "> next page </a><a Href=\ "" + HttpContext.Current.Request.CurrentExecutionFilePath + "" page= "+ allpage +" \ "> Last </a>": "Next last 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 Procedures Pagesql
Copy CodeThe code is as follows:
CREATE PROCEDURE Pagesql
@sqlSelect varchar (--select) The field in front of the from does not contain a SELECT
, the field following the @sqlFrom varchar (--from) contains the From
, @countPerPage INT--Rows of data per page
, @toPage int--page number to go to
As
BEGIN

--based on the number of rows per page and the number of pages to go to get the data starting point
Declare @start int
Declare @end int
Set @end = @countPerPage * @toPage
Set @start = @countPerPage * (@toPage-1) + 1

--temporary table names can be randomly named
Declare @tmpTable varchar (10)
SET @tmpTable = ' #tmp '
Declare @sqlStr varchar (800)
--Create a data source to a 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 to get the required data
Select @sqlStr = @sqlStr + ' + ' select ' + RTrim (@sqlSelect) + ' from ' + @tmpTable
SELECT @sqlStr = @sqlStr + ' WHERE RowIndex BETWEEN ' + Convert (char, @start) + "and" + convert (char, @end)
--Delete temporary tables
SELECT @sqlStr = @sqlStr + ' + ' DROP TABLE ' + @tmpTable
EXEC (@sqlStr)

End
Go


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.