使用AjaxPro可轻松实现Ajax功能。
不过,须谨记四步区:
1、在web.config中配置,位于<system.web>下
<httpHandlers>
<!--AJAXPRO-->
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2" />
</httpHandlers>
2、在页面后台加上特性标记,以ShowNews.aspx为例
[AjaxPro.AjaxNamespace("ColeNews")]
public partial class _ShowNews : System.Web.UI.Page
3、注册本页为AJAX页面
protected void Page_Load(object sender, EventArgs e)
{
//注册本页为AJAX
AjaxPro.Utility.RegisterTypeForAjax(typeof(_ShowNews));
}
4、为方法标记特性
[AjaxPro.AjaxMethod]
/// <summary>
/// 获得评论列表
/// </summary>
/// <param name="pageIndex">当前页码数</param>
/// <returns></returns>
public List<Comment> Getcomments(int pageIndex)
{
/****** 处理分页 **********/
PageManager pager = PageManager.GetDefaultPager();
//显示留言条数
pager.PageSize = 10;
int startRecordIndex = (pageIndex - 1) * pager.PageSize + 1;
int recordCount = 0;
List<Comment> comments = CommentManager.GetComments(startRecordIndex, pager.PageSize, out recordCount, _newsId);
pager.RecordCount = recordCount;
pager.CurrentPageIndex = pageIndex;
//加载分页信息
_pagerInfo = pager.GetNavagateInfo("javascript:LoadComment(");
/********** 处理分页完毕 ************/
return comments;
}
5、前台页面须有一个服务器端表单,这点我倒常忘。
<form id="Frm" runat="server"></form>
6、调用,js可直接操作DataSet、DataTable、IList等数据,这里结合jQuery。
var comments = ColeNews.Getcomments(pageIndex).value;
//commentsCount = comments.length;
//alert(comments.length);
//操作List<Comment>
$("#tbComments").empty();
jQuery.each(comments, function(n, comment){
var style = (n % 2 == 0) ? "class='CommentItem'" : "";
var info = "<tr " + style +">";
info += "<td style=' 500px;' class='top'><span class='commenterName'>#" + ((pageIndex - 1) * 10 + n + 1) + " " + comment.Sender + "</span> 说/said:</td>";
info += "<td class='top'><span class='commentTime'>" + comment.SendTime + "</span></td>";
info += "</tr><tr " + style + ">";
info += "<td colspan='2'>" + comment.Content + "</td></tr>";
jQuery(info).appendTo($("#tbComments"));
});
//操作List<Duty>
jQuery.each(duties, function(n){
jQuery("<option value='" + duties[n].DutyId + "'>" + duties[n].DutyName + "</option>").appendTo(selDuties);
});
//操作DataTable
var len = employees.Rows.length;
for(var n= 0;n < len; n++)
{
jQuery("<option value='" + employees.Rows[n].EmployeeId + "'>" + employees.Rows[n].EmployeeName + "</option>").appendTo(selEmployees);
}