C#應用WebService聯合jQuery完成無刷新翻頁的辦法。本站提示廣大學習愛好者:(C#應用WebService聯合jQuery完成無刷新翻頁的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#應用WebService聯合jQuery完成無刷新翻頁的辦法正文
本文實例講述了C#應用WebService聯合jQuery完成無刷新翻頁的辦法。分享給年夜家供年夜家參考。詳細以下:
1. 起首創立數據庫、表Article,字段ArticleId,Title
前台代碼
<%@ Page Language="C#" AutoEventWireup="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <script src="jquery-1.6.4.min.js" type="text/javascript"></script> </head> <body> <form id="form1" runat="server"> <div id="result"> </div> </form> <script type="text/javascript"> var pageNo = 1; //以後頁號 var pageItems = 10; //每頁顯示的行數,這個數字與da.Fill(ds,pageNo,3,"gbook");外面的3堅持分歧 var MaxPage = pageItems; function showPage(m) { if (m == -1) { if (pageNo < 2) { alert("曾經到了首頁"); return; }; MaxPage = pageItems; } else { if (MaxPage < pageItems) { alert("曾經到了末頁"); return; }; } pageNo += m; getData(); } $(document).ready(function () { getData(); }); function getData() { $.ajax({ type: "POST", cache: false, url: "WebService3.asmx/Select", /* 留意前面的名字對應CS的辦法稱號 */ data: { "pageNo": (pageNo - 1) * pageItems }, /* 留意參數的格局和稱號 */ contentType: "application/x-www-form-urlencoded", dataType: "xml", error: function (result) { alert(result.responseText); }, success: function (data) { MaxPage = $(data).find('Article').size(); /* Article是後台輸入的表稱號,要與後台對應 */ if (MaxPage == 0) { $("#result").html("沒有記載"); return; } t = "<table border='1'>"; $(data).find('Article').each(function (index, ele) { /* Article是後台輸入的表稱號,要與後台對應 */ var ArticleId = $(ele).find('ArticleId').text(); var Title = $(ele).find('Title').text(); t += "<tr>"; t += "<td>" + ArticleId + "</td>"; t += "<td>" + Title + "</td>"; t += "</tr>"; }) t += "</table>"; t += "<div><a href='' onclick='showPage(-1);return false;'>上一頁</a> <a href='' onclick='showPage(1);return false;'>下一頁</a></div>" $("#result").html(t); } }); } </script> </body> </html>
2. 後台代碼
using System.Data; using System.Data.SqlClient; using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //若要許可應用 ASP.NET AJAX 從劇本中挪用此 Web 辦事,請撤消對下行的正文 [System.Web.Script.Services.ScriptService] public class WebService3 : System.Web.Services.WebService { [WebMethod] public System.Data.DataSet Select(int pageNo) { System.Data.SqlClient.SqlConnection sqlCon = new SqlConnection(); sqlCon.ConnectionString = "server=.;uid=sa;pwd=sa;database=guestbook"; //界說SQL語句 string SqlStr = "SELECT ArticleId,Title FROM Article ORDER BY ArticleId DESC"; //實例化SqlDataAdapter對象 SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon); DataSet ds = new DataSet(); da.Fill(ds, pageNo, 10, "Article"); return ds; } }
願望本文所述對年夜家的C#法式設計有所贊助。