Database Operations:
Copy codeThe Code is as follows:
/// <Summary>
/// Obtain the total number
/// </Summary>
/// <Returns> </returns>
Public string getTotal ()
{
StringBuilder sb = new StringBuilder ();
Sb. Append ("select count (*) total from Test ");
DataTable dt = DBHelper. ExecuteDt (sb. ToString ());
Return dt. Rows [0] [0]. ToString ();
}
/// <Summary>
/// Obtain the corresponding data based on the current page number and number of entries per page.
/// </Summary>
/// <Param name = "pageNum"> Number of entries per page </param>
/// <Param name = "currentPage"> current page number </param>
/// <Returns> </returns>
Public DataTable getPagesData (int pageNum, int currentPage)
{
StringBuilder sb = new StringBuilder ();
Sb. Append ("select top" + pageNum + "* from Test where ");
Sb. Append ("ID not in (select top" + pageNum * currentPage + "ID from Test )");
Return DBHelper. ExecuteDt (sb. ToString ());
}
Front-end:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "sqlPager_Default" %>
<% @ Register Assembly = "AspNetPager" Namespace = "Wuqi. Webdiyer" TagPrefix = "webdiyer" %>
<! 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> pagination without stored procedures </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: gridview ID = "gvSql" runat = "server">
</Asp: gridview>
</Div>
<Div>
<Webdiyer: aspnetpager ID = "AspNetPager1" runat = "server" OnPageChanged = "AspNetPager1_PageChanged" PageSize = "3">
</Webdiyer: aspnetpager>
</Div>
</Form>
</Body>
</Html>
Background:
Copy codeThe Code is as follows:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Public partial class sqlPager_Default: System. Web. UI. Page
{
BLL. Test test = new BLL. Test ();
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
AspNetPager1.RecordCount = Convert. ToInt32 (test. getTotal (); // total number of records saved in this attribute ..
Bind ();
}
}
Private void Bind ()
{
This. gvSql. DataSource = test. getPagesData (Convert. ToInt32 (AspNetPager1.PageSize), AspNetPager1.CurrentPageIndex-1 );
This. gvSql. DataBind ();
}
Protected void AspNetPager1_PageChanged (object sender, EventArgs e)
{
Bind ();
}
}