加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

loner_li AJax 无刷新分页--显示数据库表内容 实例

发布时间:2020-12-15 20:56:46 所属栏目:百科 来源:网络整理
导读:前台+web服务文件 html xmlns="http://www.w3.org/1999/xhtml" head runat="server" title/title style type="text/css" img{ width:400px; height:300px;} /style script src="js/jquery-1.9.1.js" type="text/javascript"/script script type="text/javasc

前台+web服务文件

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
img{ width:400px; height:300px;}
</style>
<script src="js/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {

var pageindex=1;
var pagelastindex;

WebApplication1.WebService1.GetCount(onSuccess1,onFailed1);
function onSuccess1(result) {
pagelastindex=result;
}
function onFailed1() {
$('#divdata').html('数据获取失败');
}
WebApplication1.WebService1.GetData(pageindex,onSuccess,onFailed);
function onSuccess(result) {
$('#divdata').html(result);
}
function onFailed() {
$('#divdata').html('数据获取失败');
}

$('#btnfirst').click(function () {
pageindex = 1;
WebApplication1.WebService1.GetData(pageindex,onFailed);

})
$('#btnPre').click(function () {
if (pageindex>1) {
pageindex--;
WebApplication1.WebService1.GetData(pageindex,onFailed);
}

})
$('#btnNext').click(function () {
if (pageindex<pagelastindex) {
pageindex++;
WebApplication1.WebService1.GetData(pageindex,onFailed);
}

})
$('#btnlast').click(function () {
pageindex = pagelastindex;
WebApplication1.WebService1.GetData(pageindex,onFailed);
})
})
</script>

</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<div>
<div id="divdata">
</div>
<div>
<input id="btnfirst" type="button" value="首页" />
<input id="btnPre" type="button" value="上一页" />

<input id="btnNext" type="button" value="下一页" /><input id="btnlast"
type="button" value="最后一页" />
</div>


</div>

<div id="divarticle">
<input id="Button3" type="button" value="button" />
<img src="images/Hydrangeas.jpg" />

</div>

</form>
</body>
</html>

WebService1.asmx

public class WebService1 : System.Web.Services.WebService
{
int pagesize = 20;

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public int GetCount()
{
string strcon = @"Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=liang";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//每次都显式打开
conn.Open();
//cmd.CommandText = "SELECT T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,20)+'......' AS NewsContent,T1.CreateTime,T2.ClassName,T3.RealName FROM T_News1 T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId";
cmd.CommandText = " SELECT COUNT(*) FROM T_News1";
int totalcount = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
conn.Dispose();

int pagelastindex = 0;
if (totalcount % pagesize == 0)
{
pagelastindex = totalcount / pagesize;
}
else
{
pagelastindex = totalcount / pagesize + 1;
}
return pagelastindex;
}
[WebMethod]
public string GetData(string pageindex)
{

string strcon = @"Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=liang";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//每次都显式打开
conn.Open();
string sqlstr = "SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY T1.Id DESC)AS rownumber,T1.Id,T3.RealName FROM T_News1 T1 left JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId left JOIN T_User T3 ON T1.NewsCreator=T3.UserId)A WHERE A.rownumber>(@pageindex-1)*@pagesize AND A.rownumber<=@pageindex*@pagesize";
cmd.CommandText = sqlstr;
cmd.Parameters.AddWithValue("@pageindex",pageindex);
cmd.Parameters.AddWithValue("@pagesize",pagesize);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();

//拼接字符串
StringBuilder sb1 = new StringBuilder();
sb1.Append("<table>");
for (int i = 0; i < dt.Rows.Count; i++)
{
sb1.Append("<tr>");
sb1.Append("<td>");
sb1.Append(dt.Rows[i]["Id"].ToString());
sb1.Append("</td>");

sb1.Append("<td>");
sb1.Append(dt.Rows[i]["NewsTitle"].ToString());
sb1.Append("</td>");

sb1.Append("<td>");
sb1.Append(dt.Rows[i]["NewsContent"].ToString());
sb1.Append("</td>");

sb1.Append("<td>");
sb1.Append(dt.Rows[i]["CreateTime"].ToString());
sb1.Append("</td>");

sb1.Append("<td>");
sb1.Append(dt.Rows[i]["ClassName"].ToString());
sb1.Append("</td>");

sb1.Append("<td>"); sb1.Append(dt.Rows[i]["RealName"].ToString()); sb1.Append("</td>"); sb1.Append("</tr>"); } sb1.Append("</table>"); return sb1.ToString(); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读