Asp.net no-refreshing paging instance code

Source: Internet
Author: User

Data Code:

Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Data;
Using System. Data. SqlClient;
Using System. Collections;
Using System. Reflection;

Namespace DAL
{
Public class UserManageClass
{
/// <Summary>
/// Obtain the total number of pages
/// </Summary>
/// <Returns> total number of pages </returns>
Public int GetPageCount ()
{
Int counts;
String SqlStr = "select count (0) from [User]";
Counts = new SQLHelper (). Content (SqlStr, CommandType. Text );
Return counts;
}
/// <Summary>
/// Retrieve the content of each page
/// </Summary>
/// <Param name = "SatrPage"> start page </param>
/// <Param name = "EndPage"> end page </param>
/// <Returns> content of each page </returns>
Public DataTable GetPageDate (string SatrPage, string EndPage)
{
DataTable dt;
String SqlStr = @ "select * from
(Select *, ROW_NUMBER () over (order by id) as no _ from [User]) aa
Where aa. no _ between' "+ SatrPage +" 'and' "+ EndPage + "'";
Dt = new SQLHelper (). ExecuteQuery (SqlStr, CommandType. Text );
Return dt;
}

/// <Summary>
/// Convert a able to a list
/// </Summary>
/// <Typeparam name = "T"> Object Type </typeparam>
/// <Param name = "dt"> DataTable to be converted </param>
/// <Returns> </returns>
Public List <T> DataTableToEntityList <T> (DataTable dt)
{
List <T> entiyList = new List <T> ();

Type entityType = typeof (T );
PropertyInfo [] entityProperties = entityType. GetProperties ();

Foreach (DataRow row in dt. Rows)
{
T entity = Activator. CreateInstance <T> ();

Foreach (PropertyInfo propInfo in entityProperties)
{
If (dt. Columns. Contains (propInfo. Name ))
{
If (! Row. IsNull (propInfo. Name ))
{
PropInfo. SetValue (entity, row [propInfo. Name], null );
}
}
}

EntiyList. Add (entity );
}

Return entiyList;
}


}
}

PageService. ashx. cs General handler code:

Copy codeThe Code is as follows:
Using System;
Using System. Collections;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. Xml. Linq;
Using System. Data. SqlClient;
Using DAL;
Using System. Web. Extensions;
Using System. Web. Script. Serialization;
Using Model;
Using System. Web. UI. MobileControls;
Using System. Collections. Generic;

Namespace LandingSystem
{
/// <Summary>
/// $ Codebehindclassname $ abstract description
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
Public class PageService: IHttpHandler
{

Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "text/plain ";
String action = context. Request ["action"];
If (action = "GetPageCount ")
{
Int counts = new UserManageClass (). GetPageCount ();
Int page = counts/3;
If (counts % 3! = 0)
{
Page ++;
}
Context. Response. Write (page );
}
Else if (action = "GetPageData ")
{
Int pageNo = Convert. ToInt32 (context. Request ["PageNo"]);
String SatrPage = (pageNo-1) * 3 + 1). ToString ();
String EndPage = (pageNo * 3). ToString ();
DataTable dt = new UserManageClass (). GetPageDate (SatrPage, EndPage );
IList <RegisterModel> data = ModelConvertHelper <RegisterModel>. ConvertToModel (dt );
// IList <RegisterModel> data = new UserManageClass (). DataTableToEntityList <RegisterModel> (dt );
Var p1 = data. Select (c => new {c. Name, c. Phone });
# Region Waste Code
// Var p1 = data. Select (c => new {c. Name, c. Phone });
// Var p1 = data. Select (dr => new {dr ["Name"]. ToString (), dr ["Phone"]. ToString ()});


// Var T_model = new List <RegisterModel> ();
// Var p3 = T_model.Select (c => new {c. Name, c. Phone });

// Var p2 = data. Select (c => new {})
# Endregion
JavaScriptSerializer jss = new JavaScriptSerializer ();
Context. Response. Write (jss. Serialize (p1 ));
}
}

Public bool IsReusable
{
Get
{
Return false;
}
}
}
}

Aspx page code:

Copy codeThe Code is as follows:
<Head runat = "server">
<Title> No title page </title>

<Script src = "JS/jquery-latest.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
//-----------------------------------------------------------
Function getPageData (pageNo) {// method for obtaining a certain page of data
$. Post ("PageService. ashx", {"action": "GetPageData", "PageNo": pageNo}, function (data, status ){
If (status = "success "){
$ ("# Comment"). empty ();
Var comments = $. parseJSON (data); // deserialize json data.
For (var I = 0; I <comments. length; I ++ ){
Var row = comments [I];
Var li = $ ("<li>" + row. Name + ":" + row. Phone + "</li> ");
$ ("# Comment"). append (li); // each time a piece of data is retrieved, A li is created and appended to the Comment/ul.
}
}
});
}
//-------------------------------------------------------------------
GetPageData (1); // enter the page for the first time. The data on the first page is displayed.
//----------------------------------------------------------------/
// Retrieve all pages and initialize the pagination button
$. Post ("PageService. ashx", {"action": "GetPageCount"}, function (data, status ){
If (status = "success "){
Var tr1 = $ ("<tr> </tr> ");
Var pageNo = parseInt (data );
For (var I = 1; I <= pageNo; I ++ ){
Var td = $ ("<td> <a href =''> "+ I +" </a> </td> ");
Tr1.append (td );
}
$ ("# PageNo"). append (tr1 );
$ ("# PageNo a"). click (function (e) {// After the page number is created, a click event is monitored for each page number.
E. preventDefault (); // cancel the default redirect action of.
Getpagedata(metadata (this).html (); // click it to retrieve the page data.
});
}
});
//----------------------------------------------------------------------------
});
</Script>
</Head>
<Body>
<Table>
<Tr>
<Td>
<Ul id = "Comment"> </ul>
</Td>
</Tr>
</Table>
<Br/>
Page number:
<Table id = "pageNo"> </table>
</Body>
</Html>

ModelConvertHelper. cs (converts a able to a list generic class) code:

Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Collections;
Using System. Data;
Using System. Reflection;

Namespace DAL
{
Public class ModelConvertHelper <T> where T: new ()
{
Public static IList <T> ConvertToModel (DataTable dt)
{
IList <T> ts = new List <T> ();
Type type = typeof (T );
String tempName = "";
Foreach (DataRow dr in dt. Rows)
{
T t = new T ();
// Obtain the public attributes of this model
PropertyInfo [] propertys = t. GetType (). GetProperties ();
Foreach (PropertyInfo pi in propertys)
{
TempName = pi. Name;
// Check whether the DataTable contains this column
If (dt. Columns. Contains (tempName ))
{
// Determine whether the property has a Setter
If (! Pi. CanRead) continue;
Object value = dr [tempName];
If (value! = DBNull. Value)
If (pi. PropertyType = typeof (int ))
{
Pi. SetValue (t, Convert. ToInt32 (value), null );
}
Else if (pi. PropertyType = typeof (string ))
{
Pi. SetValue (t, value. ToString (), null );
}
// Pi. SetValue (t, value, null );
}
}
Ts. Add (t );
}
Return ts;
}


}
}

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.