准备jquery.min.js,Newtonsoft.Json.dll。
1. 文章类
public class News
{
/// <summary>
/// 文章ID
/// </summary>
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
/// <summary>
/// 文章标题
/// </summary>
private string news_title;
public string News_title
{
get { return news_title; }
set { news_title = value; }
}
/// <summary>
/// 文章类别
/// </summary>
private string news_class;
public string News_class
{
get { return news_class; }
set { news_class = value; }
}
/// <summary>
/// 文章关键字
/// </summary>
private string news_keyword;
public string News_keyword
{
get { return news_keyword; }
set { news_keyword = value; }
}
/// <summary>
/// 文章图片
/// </summary>
private string news_pic;
public string News_pic
{
get { return news_pic; }
set { news_pic = value; }
}
/// <summary>
/// 文章作者
/// </summary>
private string news_author;
public string News_author
{
get { return news_author; }
set { news_author = value; }
}
/// <summary>
/// 文章来源
/// </summary>
private string news_from;
public string News_from
{
get { return news_from; }
set { news_from = value; }
}
/// <summary>
/// 文章日期
/// </summary>
private string news_inputdata;
public string News_inputdata
{
get { return news_inputdata; }
set { news_inputdata = value; }
}
/// <summary>
/// 文章简介
/// </summary>
private string news_jianjie;
public string News_jianjie
{
get { return news_jianjie; }
set { news_jianjie = value; }
}
/// <summary>
/// 文章内容
/// </summary>
private string news_content;
public string News_content
{
get { return news_content; }
set { news_content = value; }
}
/// <summary>
/// 文章小图
/// </summary>
private string xt_pic;
public string Xt_pic
{
get { return xt_pic; }
set { xt_pic = value; }
}
}
public class NewsManager
{
public static IList<News> NewsQueryFromMToN(int count, int start)
{
cs.classbase myc = new cs.classbase();
using (SqlConnection cn = myc.GetConn())
{
cn.Open();
string sql = "SELECT TOP " + count + " f.* FROM tb_news f WHERE id NOT IN (SELECT TOP " + start + " id FROM tb_news where news_class =1 ORDER BY id desc) and f.news_class =1 ORDER BY id desc";
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataReader dr = cmd.ExecuteReader();
IList<News> list = new List<News>();
while (dr.Read())
{
News myNews = new News();
if (dr["ID"] != DBNull.Value)
{
myNews.Id = (int)dr["id"];
}
if (dr["news_title"] != DBNull.Value)
{
myNews.News_title = (string)dr["news_title"];
}
if (dr["news_content"] != DBNull.Value)
{
myNews.News_content = (string)dr["news_content"];
}
if (dr["news_author"] != DBNull.Value)
{
myNews.News_author = (string)dr["news_author"];
}
if (dr["news_inputdata"] != DBNull.Value)
{
myNews.News_inputdata = ((DateTime)dr["news_inputdata"]).ToString("yyyy-MM-dd ");
}
list.Add(myNews);
}
dr.Close();
return list;
}
}
}
3.Handler.ashx
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class Handler : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string Content = "";
string action =context.Request.QueryString["action"].ToString();
if (action == "GetMoreNews")
{
int count = int.Parse(context.Request.QueryString["count"].ToString());
int start = int.Parse(context.Request.QueryString["start"].ToString());
IList<News> morenews = NewsManager.NewsQueryFromMToN(count, start);
Content = JavaScriptConvert.SerializeObject(morenews);
}
context.Response.Write(Content);
}
public bool IsReusable {
get {
return false;
}
}
}
4.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 = "<li><a href='../article.aspx?id="+ array['Id'] +" '>" + array['News_title'] + "</a></li><br/>"
$("#more").append(str);
});
if (json == "") {
$("#more2").html("没有更多内容加载了……");
}
}
});
}
var count = 5;
var start = 0;
init(count, start);
$(".pageBtn").click(function () {
start += 5;
init(count, start);
});
});
</script>
5.html
<ul id ="more" class="newsPage">
</ul>
<div class="pageBtn" id="more2" tid="chuckyin">点击查看更多</div>