asp.net点击‘查看更多’实现无刷新加载
页面页面的js代码如下,01 <b> <script type="text/javascript">
02 $(function () {
03 function init(count, start) {
04 $.ajax({
05 type: "GET",
06 dataType: "json",
07 url: "Handler/Handler.ashx",
08 data: { action: "GetMoreNews", count: count, start: start },
09 beforeSend: function () { $("#divload").show(); $("#more2").hide(); },
10 complete: function () { $("#divload").hide(); $("#more2").show(); },
11 success: function (json) {
12 var str = "";
13 $.each(json, function (index, array) {
14 var str = "<div class='single_item'>"
15 + "<div class='element_head'>"
16 + "<div class='author'>" + array['Title'] +"</div>"
17 + "<div class='date'>" + array['Date'] + "</div>"
18 + "</div>"
19 + "<div class='content'>" + array['Contents'] + "</div>"
20 + "</div>";
21 $("#more").append(str);
22 });
23 if (json == "") {
24 $("#more2").html("没有更多内容加载了……");
25 }
26 }
27 });
28 }
29 var count = 5;
30 var start = 0;
31 init(count, start);
32 $(".get_more").click(function () {
33 start += 5;
34 init(count, start);
35 });
36 });
37 </script></b>
解释上面js的大体意思:定义一个init方法,此方法带有两个参数count和start,count意思是每次加载显示评论数,start意思是,每次从数据库中读取的位置,比如0,5,10。
Handler.ashx处理页面的代码如下
1 <b>case "GetMoreNews":
2 int count = int.Parse(context.Request.QueryString["count"].ToString());
3 int start = int.Parse(context.Request.QueryString["start"].ToString());
4 IList<WineNews> morenews = WineNewsManager.WineNewsQueryFromMToN(count,start);
5 Content = JavaScriptConvert.SerializeObject(morenews);
6 break;</b>
WineNewsQueryFromMToN代码如下
01 <b>public static IList<WineNews> WineNewsQueryFromMToN(int count,int start)
02 {
03 using (SqlConnection cn = new SqlConnection(SQLHelp.Conn))
04 {
05 cn.Open();
06 string sql = "SELECT TOP " + count + " f.* FROM tb_WineNews f
补充:Web开发 , ASP.Net ,