asp.net use JS paging to implement asynchronous loading data _ practical skills

Source: Internet
Author: User
1. Preparation work

Introduction of "Jquery-1.8.3.min.js", Ajaxpro.2.dll ": for the foreground JS call backend method.

2, the Web.config configuration
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<connectionStrings>
<clear/>
<!--database links-->
<add name= "Connswtloginlog" connectionstring= "Server=duwei\sql2005;database=swtloginlog;user id=sa;password= 111111; Connect timeout=120;pooling=true;min pool Size=5;max pool size=10 "/>
</connectionStrings>
<system.web>
<compilation debug= "True" targetframework= "4.0"/>
<!--page Invoke background method-->

<add verb= "Post,get" path= "Ajaxpro/*.ashx" type= "ajaxpro.ajaxhandlerfactory,ajaxpro.2"/>

</system.web>
</configuration>

3, directory structure


The following is directly on the code.

4, login.aspx page code
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Login.aspx.cs" inherits= "Aspnet.login"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<script src= "Js/jquery-1.8.3.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
function inittable (DT) {
var str = ' <table border= ' 1px ' > '
+ ' <tr> '
+ ' <td> '
+ ' loginID '
+ ' </td> '
+ ' <td> '
+ ' Swtid '
+ ' </td> '
+ ' <td> '
+ ' UserName '
+ ' </td> '
+ ' <td> '
+ ' IP '
+ ' </td> '
+ ' <td> '
+ ' address '
+ ' </td> '
+ ' <td> '
+ ' LogTime '
+ ' </td> '
+ ' <td> '
+ ' LogType '
+ ' </td> '
+ ' </tr> ';
for (var i = 0; i < dt.) Rows.length; i++) {
str = str + ' <tr> '
+ ' <td> '
+ dt. rows[i][' loginID ']
+ ' </td> '
+ ' <td> '
+ dt. rows[i][' Swtid ']
+ ' </td> '
+ ' <td> '
+ dt. rows[i][' UserName ']
+ ' </td> '
+ ' <td> '
+ dt. rows[i][' IP ']
+ ' </td> '
+ ' <td> '
+ dt. rows[i][' address '] + dt. rows[i][' Address2 ']
+ ' </td> '
+ ' <td> '
+ dt. rows[i][' LogTime ']
+ ' </td> '
+ ' <td> '
+ dt. rows[i][' LogType ']
+ ' </td> '
+ ' </tr> '
}
str = str + ' </table> ';
$ ("#d1"). html (str);
}
function Firtpage (page) {
$ ("#pageNo"). Text (page);
var dt = AspNet.Login.FindDate (page). value;
inittable (DT);
}
Defines a current page that is initially 1
var pageno = 1;
Total pages
var totalpage = <%=pagecount%>;
Function Showcontent (OP) {
if (op = = "a") {
PageNo = 1;
}
else if (op = = "Previous") {
if (PageNo > 1)
PageNo-= 1;
Else
PageNo = 1;
}
else if (op = = "Next") {
if (PageNo < totalPage-1)
PageNo + 1;
Else
PageNo = totalPage-1;
}
else if (op = "Last") {
PageNo = totalPage-1;
}
else if (op== "Jump") {
var jump = $ ("#jump"). Val ();
if (jump<1 | | jump>totalpage) {
PageNo = 1;
}else{
PageNo = jump;
}
}
else {
PageNo = 1;
}
Firtpage (PageNo);
}
$ (function () {
Showcontent ("a");
});
</script>

<body>
<form id= "Form1" runat= "Server" >
<div id= "D1" align= "center" ></div>
<div align= "center" >
<span id= "Sp_showcontent" >
<label id= "pageno" ></label> page | Total <%=pageCount%> page
|<a onclick= "Showcontent (' a"); "href=" javascript:void (0); " > Home </a>
|<a onclick= "showcontent (' previous ');" href= "javascript:void (0);" > Prev </a>
|<a onclick= "showcontent (' Next ');" href= "javascript:void (0);" > next Page </a>
|<a onclick= "showcontent (' last ');" href= "javascript:void (0);" > Last </a>
| Jump to <input id= "Jump"/><a onclick= "showcontent" (' Jump '); "href=" javascript:void (0); " >GO</a>
</span>
</div>
</form>
</body>


Background code
Copy code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data;
Using Aspnet.service;

Namespace AspNet
{
public partial class Login:System.Web.UI.Page
{
Test with no page 2 data
public int pageSize = 2;
public int PageCount;
Public Loginlogservice Logservice = new Loginlogservice ();
protected void Page_Load (object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax (typeof (Login));
if (! IsPostBack)
{
PageCount = Logservice.pagecount (pageSize);
}
}
Ajaxpro specific use methods can be a lot of online examples
[Ajaxpro.ajaxmethod]
Public DataTable finddate (int currentpage)
{
Return Logservice.finddate (PageSize, currentpage);
}
}
}

5, LoginLogService.cs
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Data;
Using System.Data.SqlClient;

Namespace Aspnet.service
{
public class Loginlogservice
{
Public DataTable finddate (int pageSize, int currentpage)
{

String sql = "Select Loginid,swtid,username,ip,address,address2,logtime,logtype from ("
+ "SELECT *, Row_number () over (order by loginID) as Columnnum from dbo. Loginlog) A "
+ "WHERE a.columnnum BETWEEN @begin and @end";
Sqlparameter[] Paras = new sqlparameter[]{new SqlParameter ("@begin", PageSize * (currentPage-1) +1),
New SqlParameter ("@end", PageSize * currentpage)};
DataTable dt = Dbhelper.getdataset (SQL, paras);
return Dbhelper.getdataset (SQL, paras);
}
public int PageCount (int pageSize)
{
String sql = "Select COUNT (1) from dbo." Loginlog ";
int rowcount = Int. Parse (Dbhelper.getdataset (SQL). Rows[0][0]. ToString ());
return rowcount% PageSize = 0? rowcount/pagesize:rowcount/pagesize+1;
}
}
}

6, Utils put the DBHelper.cs
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;

Namespace AspNet
{
public static Class DBHelper
{

private static SqlConnection connection;
public static SqlConnection Connection
{
Get
{
String connectionString = configurationmanager.connectionstrings["Connswtloginlog"]. ConnectionString;
if (connection = null)
{
Connection = new SqlConnection (connectionString);
Connection. Open ();
}
else if (connection. state = = System.Data.ConnectionState.Closed)
{
Connection. Open ();
}
else if (connection. state = = System.Data.ConnectionState.Broken)
{
Connection. Close ();
Connection. Open ();
}
return connection;
}
}

The ExecuteNonQuery method is used to execute inserts, deletes, UPDATE statements, and returns the number of rows affected
public static int ExecuteCommand (string safesql)
{
SqlCommand cmd = new SqlCommand (Safesql, Connection);
int result = cmd. ExecuteNonQuery ();
return result;
}

public static int ExecuteCommand (String sql, params sqlparameter[] values)
{
SqlCommand cmd = new SqlCommand (sql, Connection);
Cmd. Parameters.addrange (values);
return CMD. ExecuteNonQuery ();
}


ExecuteScalar () Returns the value of the first column after the execution of the SQL statement, object type
public static int Getscalar (string safesql)
{
SqlCommand cmd = new SqlCommand (Safesql, Connection);
int result = Convert.ToInt32 (cmd. ExecuteScalar ());
return result;
}

public static int Getscalar (String sql, params sqlparameter[] values)
{
SqlCommand cmd = new SqlCommand (sql, Connection);
Cmd. Parameters.addrange (values);
int result = Convert.ToInt32 (cmd. ExecuteScalar ());
return result;
}

ExecuteReader () Returns a DataReader object that is used for all rows that match the command, typically for reading data
public static SqlDataReader Getreader (String safesql)
{
SqlCommand cmd = new SqlCommand (Safesql, Connection);
SqlDataReader reader = cmd. ExecuteReader ();
return reader;
}

public static SqlDataReader Getreader (String sql, params sqlparameter[] values)
{
SqlCommand cmd = new SqlCommand (sql, Connection);
Cmd. Parameters.addrange (values);
SqlDataReader reader = cmd. ExecuteReader ();
return reader;
}

public static DataTable GetDataSet (String safesql)
{
connection = connection;
DataSet ds = new DataSet ();
SqlCommand cmd = new SqlCommand (Safesql, Connection);
SqlDataAdapter da = new SqlDataAdapter (cmd);
Da. Fill (DS);
Cmd. Parameters.clear ();
Return DS. Tables[0];
}

public static DataTable GetDataSet (String sql, params sqlparameter[] values)
{
DataSet ds = new DataSet ();
SqlCommand cmd = new SqlCommand (sql, Connection);
Cmd. Parameters.addrange (values);
SqlDataAdapter da = new SqlDataAdapter (cmd);
Da. Fill (DS);
Cmd. Parameters.clear ();
Return DS. Tables[0];
}

}
}

Copy Code code as follows:

Datasheet structure: <pre code_snippet_id= "274427" snippet_file_name= "blog_20140404_6_6418355" name= "code" class= "plain" > CREATE TABLE [dbo]. [Loginlog] (
[loginID] [INT] IDENTITY (1,1) not NULL,
[Swtid] [INT] Null
[UserName] [nvarchar] (255) COLLATE chinese_prc_ci_as NULL,
[IP] [nvarchar] (m) COLLATE chinese_prc_ci_as NULL,
[Address] [nvarchar] (255) COLLATE chinese_prc_ci_as NULL,
[Address2] [nvarchar] (255) COLLATE chinese_prc_ci_as NULL,
[LogTime] [DateTime] Null
[LogType] [INT] NULL CONSTRAINT [Default_loginlog_logtype] DEFAULT ((1)),
CONSTRAINT [Pk_loginlog_loginid] PRIMARY KEY CLUSTERED
(
[loginID] Asc
With (Pad_index = off, Ignore_dup_key = off) on [PRIMARY]
) on [primary]</pre>
<pre></pre>
<pre code_snippet_id= "274427" snippet_file_name= "blog_20140404_6_6418355" name= "code" class= "CSharp" ><pre Code_snippet_id= "274427" snippet_file_name= "blog_20140404_6_6418355" name= "code" class= "SQL" ><pre Code_ Snippet_id= "274427" snippet_file_name= "blog_20140404_6_6418355" ></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>

</pre></pre>

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.