asp.net点击 查看更多 实现无刷新加载的实现代码

时间:2021-12-07 16:54:07

页面页面的js代码如下, 

复制代码代码如下:


<script type="text/javascript"> 
$(function () { 
function init(count, start) { 
$.ajax({ 
type: "GET", 
dataType: "json", 
url: "Handler/Handler.ashx", 
data: { action: "GetMoreNews", count: count, start: start }, 
beforeSend: function () { $("#divload").show(); $("#more2").hide(); }, 
complete: function () { $("#divload").hide(); $("#more2").show(); }, 
success: function (json) { 
var str = ""; 
$.each(json, function (index, array) { 
var str = "<div class='single_item'>" 
+ "<div class='element_head'>" 
+ "<div class='author'>" + array['Title'] +"</div>" 
+ "<div class='date'>" + array['Date'] + "</div>" 
+ "</div>" 
+ "<div class='content'>" + array['Contents'] + "</div>" 
+ "</div>"; 
$("#more").append(str); 
}); 
if (json == "") { 
$("#more2").html("没有更多内容加载了……"); 


}); 

var count = 5; 
var start = 0; 
init(count, start); 
$(".get_more").click(function () { 
start += 5; 
init(count, start); 
}); 
}); 
</script> 


解释上面js的大体意思:定义一个init方法,此方法带有两个参数count和start,count意思是每次加载显示评论数,start意思是,每次从数据库中读取的位置,比如0,5,10。 
Handler.ashx处理页面的代码如下 

复制代码代码如下:


页面页面的js代码如下, 
<b> <script type="text/javascript"> 
$(function () { 
function init(count, start) { 
$.ajax({ 
type: "GET", 
dataType: "json", 
url: "Handler/Handler.ashx", 
data: { action: "GetMoreNews", count: count, start: start }, 
beforeSend: function () { $("#divload").show(); $("#more2").hide(); }, 
complete: function () { $("#divload").hide(); $("#more2").show(); }, 
success: function (json) { 
var str = ""; 
$.each(json, function (index, array) { 
var str = "<div class='single_item'>" 
+ "<div class='element_head'>" 
+ "<div class='author'>" + array['Title'] +"</div>" 
+ "<div class='date'>" + array['Date'] + "</div>" 
+ "</div>" 
+ "<div class='content'>" + array['Contents'] + "</div>" 
+ "</div>"; 
$("#more").append(str); 
}); 
if (json == "") { 
$("#more2").html("没有更多内容加载了……"); 


}); 

var count = 5; 
var start = 0; 
init(count, start); 
$(".get_more").click(function () { 
start += 5; 
init(count, start); 
}); 
}); 
</script></b> 
解释上面js的大体意思:定义一个init方法,此方法带有两个参数count和start,count意思是每次加载显示评论数,start意思是,每次从数据库中读取的位置,比如0,5,10。 
Handler.ashx处理页面的代码如下 
[code] 
case "GetMoreNews": 
int count = int.Parse(context.Request.QueryString["count"].ToString()); 
int start = int.Parse(context.Request.QueryString["start"].ToString()); 
IList<WineNews> morenews = WineNewsManager.WineNewsQueryFromMToN(count,start); 
Content = JavaScriptConvert.SerializeObject(morenews); 
break; 


WineNewsQueryFromMToN代码如下 

复制代码代码如下:


public static IList<WineNews> WineNewsQueryFromMToN(int count,int start) 

using (SqlConnection cn = new SqlConnection(SQLHelp.Conn)) 

cn.Open(); 
string sql = "SELECT TOP " + count + " f.* FROM tb_WineNews f WHERE Id NOT IN (SELECT TOP " + start + " Id FROM tb_WineNews ORDER BY Id desc) ORDER BY Id desc"; 
SqlCommand cmd = new SqlCommand(sql, cn); 
SqlDataReader dr = cmd.ExecuteReader(); 
IList<WineNews> list = new List<WineNews>(); 
while (dr.Read()) 

WineNews wineNews = new WineNews(); 
if (dr["ID"] != DBNull.Value) 

wineNews.ID = (int)dr["ID"]; 

if (dr["Title"] != DBNull.Value) 

wineNews.Title = (string)dr["Title"]; 

if (dr["Contents"] != DBNull.Value) 

wineNews.Contents = (string)dr["Contents"]; 

if (dr["Picture"] != DBNull.Value) 

wineNews.Picture = (string)dr["Picture"]; 

if (dr["Date"] != DBNull.Value) 

wineNews.Date = ((DateTime)dr["Date"]).ToString("yyyy-MM-dd HH:mm:ss"); 

list.Add(wineNews); 

dr.Close(); 
return list; 

作者:陈赛