Adapted from PHP version, original:
jquery+php implementation to browse more content http://www.helloweba.com/view-blog-130.html
Here is a record of the implementation under. Net
First, create a database table test, and insert some test data:
Copy Code code as follows:
Go
if exists (select * from sysobjects where name= ' test ')
drop table [Test]
Go
CREATE TABLE [Test] (
[ID] [int] IDENTITY (1,1),
[Author] [varchar] (50),
[Content] [varchar] (2000),
[Createon] [DateTime]
)
declare @index int;
Set @index = 1;
while (@index < 1000)
Begin
INSERT into Test ([Author],[content],[createon])
Values
(' Author ' + CAST (@index as varchar (4)), ' content ' + cast (@index as varchar (4)), DATEADD (Day, @index, GETDATE ()))
Set @index = @index + 1
End
Go
Second, create an HTML file
Copy Code code as follows:
<! 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.4.4.min.js" type= "Text/javascript" ></script>
<script src= "Js/jquery.more.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
$ (' #more '). More ({' adress ': ' ajax/data.ashx '})//Here is the request address for the back desk
});
</script>
<style type= "Text/css" >
More
{
margin:10px Auto;
width:560px;
border:1px solid #999;
}
. Single_item
{
padding:20px;
border-bottom:1px dotted #d3d3d3;
}
. author
{
Position:absolute;
left:0px;
Font-weight:bold;
Color: #39f;
}
. Createon
{
Position:absolute;
right:0px;
Color: #999;
}
. Content
{
line-height:20px;
Word-break:break-all;
}
. element_head
{
width:100%;
position:relative;
height:20px;
}
. get_more
{
margin:10px;
Text-align:center;
}
. More_loader_spinner
{
width:20px;
height:20px;
margin:10px Auto;
Background:url (loader.gif) no-repeat;
}
</style>
<body>
<div id= "More" >
<div class= "Single_item" >
<div class= "Element_head" >
<div class= "Createon" >
</div>
<div class= "Author" >
</div>
</div>
<div class= "Content" >
</div>
</div>
<a href= "javascript:;" class= "Get_more":: Click to load more content::</a>
</div>
</body>
third, establish a general processing procedure Data.ashx
Copy Code code as follows:
<%@ WebHandler language= "C #" class= "Data"%>
Using System;
Using System.Web;
Using Microsoft.Practices.EnterpriseLibrary.Data;
public class Data:ihttphandler {
public void ProcessRequest (HttpContext context) {
Context. Response.ContentType = "Text/plain";
int last = Convert.ToInt32 (context. Request.params["Last"]); Number of start records
int amount = Convert.ToInt32 (context. request.params["Amount"]); Number of records to display on a single occasion
Database db = Databasefactory.createdatabase ();
Here the Date field Createon first format, otherwise it will generate a similar format/date (1310292162507)/
String sql = string. Format ("Select Id,author,content,convert (varchar), Createon,) Createon from (select Row_number () D) as rownum,* from test as T "
+ "where rownum>{0} and Rownum<={1}", last, last + amount);
System.Data.DataTable dt = db. ExecuteDataset (System.data.commandtype.text,sql). Tables[0];
Context. Response.Write (Jsonhelper.datatabletojson (DT)); Output JSON format
}
public bool IsReusable {
get {
return false;
}
}
}
attached: jquery.more.js plugin content
Copy Code code as follows:
(function ($) {
var target = null;
var template = null;
var lock = false;
var variables = {
' Last ': 0
}
var settings = {
' Amount ': ' 10 ',
' Address ': ',
' Format ': ' JSON ',
' Template ': '. Single_item ',
' Trigger ': '. Get_more ',
' Scroll ': ' false ',
' Offset ': ' 100 ',
' Spinner_code ': '
}
var methods = {
Init:function (options) {
Return This.each (function () {
if (options) {
$.extend (settings, options);
}
Template = $ (this). Children (settings.template). Wrap (' <div/> '). parent ();
Template.css (' Display ', ' none ')
$ (this). Append (' <div class= more_loader_spinner ' > ' +settings.spinner_code+ ' </div> ')
$ (this). Children (settings.template). Remove ()
target = $ (this);
if (Settings.scroll = = ' false ') {
$ (this). Find (Settings.trigger). bind (' Click.more ', methods.get_data);
$ (this). More (' get_data ');
}
else{
if ($ (this). Height () <= $ (this). attr (' scrollheight ')) {
Target.more (' Get_data ', settings.amount*2);
}
$ (this). bind (' Scroll.more ', methods.check_scroll);
}
})
},
Check_scroll:function () {
if ((Target.scrolltop () +target.height () +parseint (settings.offset)) >= target.attr (' scrollheight ') && Lock = = False) {
Target.more (' Get_data ');
}
},
Debug:function () {
var debug_string = ';
$.each (variables, function (k,v) {
Debug_string + + ': ' +v+ ' \ n ';
})
alert (debug_string);
},
Remove:function () {
Target.children (Settings.trigger). Unbind ('. more ');
Target.unbind ('. More ')
Target.children (Settings.trigger). Remove ();
},
Add_elements:function (data) {
Alert (' adding elements ')
var root = target
Alert (root.attr (' id '))
var counter = 0;
if (data) {
$ (data). each (function () {
counter++
var t = Template
$.each (this, function (key, value) {
if (T.find ('. ') +key)) T.find ('. ') +key). Text (value);
})
T.ATTR (' id ', ' more_element_ ' + (variables.last++))
if (Settings.scroll = = ' true ') {
Root.append (t.html ())
Root.children ('. More_loader_spinner '). Before (t.html ())
}else{
Alert (' ... ')
Root.children (Settings.trigger). Before (t.html ())
}
Root.children (settings.template+ ': Last '). attr (' id ', ' more_element_ ' + ((variables.last++) +1))
})
}
else Methods.remove ()
Target.children ('. More_loader_spinner '). CSS (' Display ', ' none ');
if (Counter < Settings.amount) Methods.remove ()
},
Get_data:function () {
Alert (' Getting Data ')
var Ile;
Lock = true;
Target.children (". More_loader_spinner"). CSS (' Display ', ' block ');
$ (Settings.trigger). CSS (' Display ', ' none ');
if (typeof (Arguments[0]) = = ' number ') ile=arguments[0];
else {
ile = Settings.amount;
}
$.post (settings.address, {
Last:variables.last,
Amount:ile
}, function (data) {
$ (Settings.trigger). CSS (' Display ', ' block ')
Methods.add_elements (data)
lock = false;
}, Settings.format)
}
};
$.fn.more = function (method) {
if (Methods[method])
Return methods[Method].apply (this, Array.prototype.slice.call (arguments, 1));
else if (typeof method = = ' object ' | |!method)
Return methods.init.apply (this, arguments);
else $.error (' method ' + method + ' does not exist! ');
}
}) (JQuery)