<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(); } }
原文链接:https://www.f2er.com/ajax/166397.html