前台代码
<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;
WebApplication1.WebService1.GetData(pageindex, onSuccess, onFailed);
function onSuccess(result) {
$('#divdata').html(result);
}
function onFailed() {
$('#divdata').html('数据获取失败');
}
$('#btnNext').click(function () {
pageindex++;
WebApplication1.WebService1.GetData(pageindex, onSuccess, onFailed);
})
$('#Button3').click(function () {
$('img').hide(6000);
})
})
</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="Button1" type="button" value="首页" /><input id="Button2" type="button"
value="上一页" /><input id="btnNext" type="button" value="下一页" /><input id="Button4"
type="button" value="最后一页" />
</div>
</div>
</form>
</body>
</html>
下面的代码是写在WebService1.asmx当中的
[WebMethod]
public int GetCount()
{
string strcon = @"Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=***";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//每次都显式打开
conn.Open();
//cmd.CommandText = "SELECT T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,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=***";
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,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,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)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();
}
下面是效果,点击分页页面不在刷新。