asp.net 使用ajax分页
2013-03-06 17:35
120 查看
查询出数据,使用sql分页。
前台 jquery部分
public static List<Topic> GetTopicList(int page, int pagesize) { string sql = "select * from (select *,row_number() over (order by id) as r_num from Topic) as rum_table where r_num between (@page-1) * @pagesize+1 and @pagesize * @page"; List<Topic> list = new List<Topic>(); using (SqlConnection connection = (SqlConnection)DBHelper.GetDataBase()) { try { SqlCommand cmd = new SqlCommand(sql, connection); SqlParameter[] parameter = new SqlParameter[] { new SqlParameter("@page",page), new SqlParameter("@pagesize",pagesize) }; connection.Open(); cmd.Parameters.AddRange(parameter); SqlDataReader reader = cmd.ExecuteReader(); list = CycleErgodic(reader); connection.Close(); } catch (Exception ex) { ExceptionManage.WriteLogFile(ex.ToString()); throw new Exception("The system is buys,please try again later..."); } } return list; }经过BLL层 在 一般处理程序中 输出表格
if (context.Request.Form["getType"].ToString() == "1") { if (context.Request.Form["currentPage"] == null || context.Request.Form["pagesize"] == null) { context.Response.Write("parameter error"); return; } int currentPage = Convert.ToInt32(context.Request.Form["currentPage"]); int size = Convert.ToInt32(context.Request.Form["pagesize"]); List<Topic> list = TopicManager.GetTopicList(currentPage,size); StringBuilder strTable = new StringBuilder(); strTable.Append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"text-align: center; margin: auto\">"); strTable.Append("<tr>"); strTable.Append("<th>主题编号</th><th>标题</th><th>主题内容</th></tr>"); int num = 0; foreach (Topic topic in list) { num++; string content = topic.Content.Length > 8 ? topic.Content.Substring(0, 8) + "<a onclick=\"queryDetails(" + topic.Id + ")\" href=\"javascript:void(0)\" title='查看详情'>[查看详情]</a>" : topic.Content; string topicTitle = topic.TopicTitle.Length > 12 ? topic.TopicTitle.Substring(0, 12) + "..." : topic.TopicTitle; if (num % 2 == 0) { strTable.Append(String.Format("<tr onmouseover=\"$(this).css('background-color','#FFFFCC')\" onmouseout=\"$(this).css('background-color','#E5E5E5')\" style=\"background-color: #E5E5E5\"><td>{0}</td><td><a href=\"comment.aspx?id={1}\" title=\"{2}\">{3}</a></td><td>{4}</td></tr>", num, topic.Id, topic.TopicTitle, topicTitle, content)); } else { strTable.Append(String.Format("<tr onmouseover=\"$(this).css('background-color','#FFFFCC')\" onmouseout=\"$(this).css('background-color','#F5F5F5')\" style=\"background-color: #F5F5F5\"><td>{0}</td><td><a href=\"comment.aspx?id={1}\" title=\"{2}\">{3}</a></td><td>{4}</td></tr>", num, topic.Id, topic.TopicTitle, topicTitle, content)); } } strTable.Append("</table>"); strTable.Append("<table width=\"80%\"><tr><td align=\"right\"><a id=\"prev\" onclick=\"loadPrev()\" title=\"PageUp\" href=\"javascript:void(0)\">上一页</a> <a id=\"next\" onclick=\"loadNext()\" title=\"PageDown\" href=\"javascript:void(0)\">下一页</a> 第<span id=\"currentPage\"></span>页 共<span id=\"countPage\"></span>页</td></tr></table>"); int count = TopicManager.GetCount(); //将查询数来的总页数存入隐藏域 strTable.Append(String.Format("<input type=\"hidden\" id=\"count\" value={0} />", count)); context.Response.Write(strTable.ToString()); }
前台 jquery部分
var currentPage = 1; var pagesize = 20; var pageCount; //将当前页和页面大小提交给 一般处理程序 然后将返回的结果显示。 function loadData(currentPage, pagesize) { $.ajax({ url: "loadTable.ashx", data: { "getType": "1", "currentPage": currentPage, "pagesize": pagesize, "t": Math.random() }, type: "post", success: function (data) { $("div#topicTable").html(data); var count = $("#count").val(); //算出总页数 if (count % pagesize == 0) { pageCount = count / pagesize; } else { pageCount = parseInt((count / pagesize), 10) + 1; } $("#currentPage").html(currentPage); $("#countPage").html(pageCount); //当前页==1时,隐藏上一页按钮 if (currentPage == 1) { $("#prev").hide(); } //当前页 == 最大页 是,以藏下一页按钮 if (currentPage == pageCount) { $("#next").hide(); } } }); } //下一页 function loadNext() { currentPage = currentPage + 1; loadData(currentPage, pagesize); } //上一页 function loadPrev() { currentPage = currentPage - 1; loadData(currentPage, pagesize); } $(document).ready(function () { loadData(1,pagesize); }
<div id="topicTable"> </div>
相关文章推荐
- FCKeditor 在ASP.Net 中的使用说明
- Asp.net中的Popup控件的使用方法
- 在ASP.NET下使用jQuery UI下的Dialog产生提交空值问题
- Ext.Net学习笔记24:在ASP.NET MVC中使用Ext.Net
- ASP.NET中使用Treeview和XML
- 在ASP.NET MVC中使用“RadioButtonList”和“CheckBoxList”
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【三】——Web Api入门
- [译]ABP框架使用AngularJs,ASP.NET MVC,Web API和EntityFramework构建N层架构的SPA应用程序
- asp.net 打印控件使用方法
- asp.net中ListView控件动态绑定数据源后,使用DataPager出现错误的原因及解决办法
- ASP.NET中的阶乘的使用
- ASP.NET Oracle数据库使用事务时注意事项
- ASP.net中DynamicImage控件的使用方法
- asp.net mvc 使用Ajax调用Action 返回数据。
- Asp.Net MVC 之 Autofac 初步使用3 集成web api
- asp.net控件的使用1
- asp.net web api - 使用多个Xml文件显示帮助文档
- 在ASP.NET 2.0中操作数据之五十六:使用ObjectDataSource缓存数据
- IIS网站或系统验证码不显示问题——"使用了托管的处理程序,但是未安装或未完整安装 ASP.NET"
- ASP.NET使用AJAX联动