C#使用WebService结合jQuery实现无刷新翻页的方法
作者:songguo 时间:2022-02-23 12:00:32
本文实例讲述了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#程序设计有所帮助。
标签:C#,WebService,jQuery
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Flutter实现心动的动画特效
2021-09-03 18:08:01
![](https://img.aspxhome.com/file/2023/5/119085_0s.png)
在Unity中捕捉Android的常用按钮返回事件
2023-01-31 00:11:36
详解Android中Intent的使用方法
2023-08-02 04:40:29
![](https://img.aspxhome.com/file/2023/2/122082_0s.png)
Java进程cpu占用过高问题解决
2021-08-09 00:16:59
![](https://img.aspxhome.com/file/2023/7/70337_0s.jpg)
使用JMX监控Zookeeper状态Java API
2023-05-14 02:27:26
BeanUtils.copyProperties使用总结以及注意事项说明
2023-06-27 18:06:18
关于Java整合RocketMQ实现生产消费详解
2022-12-31 07:44:34
![](https://img.aspxhome.com/file/2023/2/80932_0s.png)
浅析java中的取整(/)和求余(%)
2023-04-30 23:46:23
IDEA快速搭建spring boot项目教程(Spring initializr)
2023-08-17 21:11:16
![](https://img.aspxhome.com/file/2023/0/58060_0s.png)
Android实现IM多人员组合的群组头像
2023-05-24 06:39:54
![](https://img.aspxhome.com/file/2023/9/122229_0s.jpg)
Android Studio 代码导航快捷键
2023-09-28 09:58:11
![](https://img.aspxhome.com/file/2023/1/117161_0s.gif)
JDK1.8中ArrayList是如何扩容的
2023-01-20 19:04:47
![](https://img.aspxhome.com/file/2023/0/62700_0s.png)
Spring Boot启动时调用自己的非web逻辑
2022-02-15 11:40:10
Spring中自动注入的两种方式总结
2022-10-24 08:54:14
![](https://img.aspxhome.com/file/2023/4/60614_0s.jpg)
C#对集合进行排序
2022-06-10 02:08:03
![](https://img.aspxhome.com/file/2023/4/111204_0s.jpg)
ibatis学习之搭建Java项目
2021-11-02 00:35:29
python 转换 Javascript %u 字符串为python unicode的代码
2022-07-28 03:15:14
SpringBoot使用Redisson实现分布式锁(秒杀系统)
2022-07-17 05:15:41
![](https://img.aspxhome.com/file/2023/7/83487_0s.jpg)
完美解决单例设计模式中懒汉式线程安全的问题
2021-12-30 01:54:21
MyBatis全局映射文件实现原理解析
2021-06-21 17:01:29