How to Implement Datalist in asp.net using digital Paging

Source: Internet
Author: User

Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Test (Datalist digital paging). aspx. cs" Inherits = "Test_Datalist digital pagination _" %>
<! 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:
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 page_: 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 of PageNums
/// </Summary>
Public class PageNums
{
/// </Summary>
/// <Param name = "ds"> DataSet instance </param>
/// <Param name = "datalistname"> DataList name </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 ;}
// Calculate the total number of pages
If (pagesize! = 0)
{
Allpage = (total/pagesize); // calculate the total number of pages
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; // The start Number of the intermediate page
// End number of the intermediate page
Endcount = page <5? 10: page + 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
Pagestr = "Total" + allpage + "page ";
Pagestr + = page> 1? "<A href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = 1 \ "> homepage </a>; <a href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ pre +" \ "> previous page </a>": "Previous page ";
// Intermediate page processing, which increases the time complexity and reduces 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 +" \ "> next page </a> <a href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ allpage +" \ "> last page </a>": "last page of the next 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 the total number of pages
If (pagesize! = 0)
{
Allpage = (total/pagesize); // calculate the total number of pages
Allpage = (total % pagesize )! = 0? Allpage + 1: allpage );
Allpage = (allpage = 0? 1: allpage );
}
Next = page + 1;
Pre = page-1;
Startcount = page/5*5; // start Number of the intermediate page
// End number of the intermediate page
Endcount = startcount + 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
Pagestr = "<a>" + page + "/" + allpage + "page </a> ";
Pagestr + = page> 1? "<A href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = 1 \ "> homepage </a> <a href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ pre +" \ "> previous page </a>": "Previous page ";
// Intermediate page processing, which increases the time complexity and reduces 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 +" \ "> next page </a> <a href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ allpage +" \ "> last page </a>": "last page of the next 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
Copy codeThe Code is as follows:
Create procedure pagesql
@ SqlSelect varchar (800) -- the field before FROM After SELECT does not need to include SELECT
, @ SqlFrom varchar (800) -- the field after FROM contains
, @ CountPerPage int -- number of data rows per page
, @ ToPage int -- the page number to be transferred
AS
BEGIN

-- Get the data start and end points based on the number of data lines per page and the page number to be transferred
Declare @ start int
Declare @ end int
Set @ end = @ countPerPage * @ toPage
Set @ start = @ countPerPage * (@ toPage-1) + 1

-- Temporary table names can be named randomly.
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) + 'input' + @ tmpTable
SELECT @ sqlStr = @ sqlStr + rtrim (@ sqlFrom)
-- Query the temporary table to obtain 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 a temporary table
SELECT @ sqlStr = @ sqlStr + ''+ 'drop table' + @ tmpTable
EXEC (@ sqlStr)

END
GO

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.