当前位置:主页 > 查看内容

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

发布时间:2021-08-20 00:00| 位朋友查看

简介:新建一般处理程序 public class Temp{ public int Index { get; set; } public string Description { get; set; } public string ImagePath { get; set; } public DateTime MyDate { get; set; }}//数据源 ListTemp listTemp = new ListTemp() { new Temp(){……

新建一般处理程序

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获取的时间值需做一些处理转换成标准日期格式
//详见http://msdn.microsoft.com/zh-cn/library/system.web.script.serialization.javascriptserializer.aspx

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


原文链接:https://m.jb51.net/article/99482.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:在ASP.NET Core中显示自定义的错误页面 下一篇:没有了

推荐图文


随机推荐