asp.net點擊 查看更多 實現無重新整理載入的實現代碼

來源:互聯網
上載者:User

頁面頁面的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;
}
}

運行效果如下

作者:陳賽

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.