$.getJSON("Photolist.ashx",function (msg){
//每次加载之前清空
$("#content").html("");
//动态创建table
var table=$("<table id='pList'></table>");
//添加表头
table.append($("<tr><th>序号</th><th>名字</th><th>照片</th><th>点击次数</th><th>支持</th><th>反对</th><th>时间</th><th>操作</th></tr>"));
for(var i=0;i<msg.length;i++){
var photo
=msg[i];
var tr=$("<tr ></tr>");
var td=$("<td>"+(i+1)+"</td>");
tr.append(td);
var time=new Date().getTime();
td=$("<td><a href='DetailPhoto.aspx?id="+photo.PId+"&time="+time+"'>"+photo.PTitle+"</a></td>");
tr.append(td);

td =$("<td><img src='ImgSmall.ashx?img=" + photo.PUrl + "' /></td>");
tr.append(td);

td=$("<td>"+photo.PClicks+"</td>");
tr.append(td);

td=$("<td><a href='' pid='"+ photo.PId +"'><img src='images/85.gif' /></a>(<span>"+photo.PUp+"</span>)</td>");
tr.append(td);
//点击支持的事件
td.children("a").click(function (){
//防止其他浏览器不支持
var id=$(this).attr("pid");

var span=$(this).next();
//请求一般处理程序,给支持+1
//Math.random()
$.get("UpOrDownPhoto.ashx?id=" +id+ "&type=up&time="+ new Date().getTime() ,function (msg){
if(msg=="1"){
//成功 更新页面上的span标签中的值
var n=parseInt(span.text())+1;
span.text(n);
}else if(msg=="0"){
alert("更新失败");
}
})
return false ;

});

td=$("<td><a href='' pid='" + photo.PId + "'><img src='images/86.gif' /></a>(<span>"+photo.PDown+"</span>)</td>");
tr.append(td);
//为反对添加事件
td.children("a").click(function (){
var id=$(this).attr("pid");
var span=$(this).next();
//请求一般处理程序反对+1
$.get("UpOrDownPhoto.ashx?id="+id+"&type=down&time="+new Date().getTime() ,function (msg){
if(msg=="1"){
//成功 更新页面上的span标签中的值
var n=parseInt(span.text())+1;
span.text(n);
}else if(msg=="0"){
alert ("更新失败");
}
});
//不让它转向,刷新
return false ;
});

// td=$("<td >"+ChangeDateFormat(photo.PTime)+"</td>");
// tr.append(td);

td=$("<td ><a href='' pid='"+photo.PId+"' url='"+photo.PUrl+"'>删 除</a></td>");
tr.append(td);
//添加删除事件
td.children("a").click(function (){
var id=$(this).attr("pid");
var url=$(this).attr("url");
var link=$(this);
$.get("DeletePhtoto.ashx?id="+id+"&time="+new Date().getTime(),function(msg){
if (msg=="1"){
alert ("删除成功");
//注意:有评论,即有外键约束的不能删除
//删除a标签所在的行tr
//link.parent().parent().remove();
//删除以后重新加载
//getpage(pageIndex);
}else if(msg=="0"){
alert ("删除失败");
}

})
})
td=$("<td ><a href='EditPhoto.ashx?id="+photo.PId+"'>删 除</a></td>");
tr.append(td);
table.append(tr);

}
//table添加到div中
$("#content").append(table);
});

cs:

public class Photolist : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write(GetTable());
}
protected string GetTable()
{
MyPhotoList.BLL.Photos bll = new MyPhotoList.BLL.Photos();
List<MyPhotoList.Model.Photos> list = bll.GetModelList("");
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(list);

}

使用之前必须添加命名空间using System.Web.Script.Serialization;



posted on 2011-12-01 22:47  幻想时空  阅读(472)  评论(0编辑  收藏  举报