Asp.net click 'view more 'to implement refreshing

Source: Internet
Author: User

The js Code on the page is as follows,
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 ("no more content loaded ...... ");
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>
To explain the general meaning of js above: Define an init method. This method has two parameters: count and start. count indicates the number of comments displayed each time, and start indicates that, the position of each read from the database, for example, 0, 5, 10.
The code for Handler. ashx to process the page is as follows:
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>
The WineNewsQueryFromMToN code is as follows:
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 WHERE Id not in (select top "+ start +" Id FROM tb_WineNews order by Id desc) order by Id desc ";
07 SqlCommand cmd = new SqlCommand (SQL, cn );
08 SqlDataReader dr = cmd. ExecuteReader ();
09 IList <WineNews> list = new List <WineNews> ();
10 while (dr. Read ())
11 {
12 WineNews wineNews = new WineNews ();
13 if (dr ["ID"]! = DBNull. Value)
14 {
15 wineNews. ID = (int) dr ["ID"];
16}
17 if (dr ["Title"]! = DBNull. Value)
18 {
19 wineNews. Title = (string) dr ["Title"];
20}
21 if (dr ["Contents"]! = DBNull. Value)
22 {
23 wineNews. Contents = (string) dr ["Contents"];
24}
25 if (dr ["Picture"]! = DBNull. Value)
26 {
27 wineNews. Picture = (string) dr ["Picture"];
28}
29 if (dr ["Date"]! = DBNull. Value)
30 {
31 wineNews. Date = (DateTime) dr ["Date"]). ToString ("yyyy-MM-dd HH: mm: ss ");
32}
33 list. Add (wineNews );
34}
35 dr. Close ();
36 return list;
37}
38} </B>
The running effect is as follows:
 
 
Author: Chen sai

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.