当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net实现无刷新调用后台实体类数据并以Json格式返回

Asp.net实现无刷新调用后台实体类数据并以Json格式返回

2017年12月12日  | 移动技术网IT编程  | 我要评论

延边音乐网,重生之无限精彩,泷泽kanon

新建一般处理程序

public class temp
{
  public int index { get; set; }
  public string description { get; set; }
  public string imagepath { get; set; }
  public datetime mydate { get; set; }
}

//数据源 
 list<temp> listtemp = new list<temp>()
 {
   new temp(){ index=1,imagepath="desert.jpg", description="图片1",mydate=datetime.now},
   new temp(){index=2,imagepath="chrysanthemum.jpg", description="图片2",mydate=datetime.now},
   new temp(){index=3,imagepath="penguins.jpg", description="图片3",mydate=datetime.now},
   new temp(){index=4,imagepath="jellyfish.jpg", description="图片4",mydate=datetime.now},
   new temp(){index=5,imagepath="tulips.jpg", description="图片5",mydate=datetime.now}
 };
 
 public void processrequest(httpcontext context)
 {
   string index = context.request["index"];
   string jsonstr = string.empty;
   foreach (var item in listtemp)
   {
     if (item.index.tostring() == index)
     {
       javascriptserializer serializer = new javascriptserializer();
       jsonstr = serializer.serialize(item); //序列化为json格式
       break;
     }
   }
 
   context.response.write(jsonstr);
 }

前台js代码

$.getjson("imagechange.ashx", { index: i - 1 }, function (result) {
  $("#<%=lbldescription.clientid %>").text(result.description);
$("#<%=image1.clientid %>").attr("src", path + result.imagepath.substr(result.imagepath.lastindexof('/') + 1));
 
  var d = eval("new " + result.mydate.replace(/\//g, ""));
          $("#<%=lbldate.clientid %>").text(todate(d.tolocaltime().tostring()));
});

前台js代码
//tolocaltime()将utc格式数据转换成标准日期格式
//注意javascriptserializer会将日期序列号为自1970年1月1号的刻度值,所以js获取的时间值需做一些处理转换成标准日期格式
//详见

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,同时也希望多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网