前台:
//绑定详细内容 function clientInfoShow(clientId,types) { if (!clientId) { return; } $.getJSON("../ajax/MoneyInfoHandler.ashx", { "clientId": clientId,"types":types,"beginDate":$("#txtTime").val(),"endDate":$("#txtEndTime").val() }, function(data) { if (data) { //清空div内容 $("#jexxInfo").empty(); var jsonTextDiv = document.getElementById("jexxInfo"); var str="<table width='100%' class='align_c'><tr><td>类型</td><td>金额(万元)</td></tr>"; for (var i = 0; i < data.length; i++) { var item = data[i]; str += ("<tr>"); str+="<td> "+item.leixing + "</td><td>" + (item.totMoney)/10000 + "</td>"; } str+="</table>" //添加容器str到jsontextdiv $(jsonTextDiv).append(str); if (data) { //开启弹出层 $.blockUI({ message: $('#wind') }); $('#btnCancle').show(); } } }); } $('#btnCancle').click(function() { $.unblockUI(); });
后台(一般处理事件):
<%@ WebHandler Language="C#" Class="MoneyInfoHandler" %> using System; using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Web; using System.Data; using System.Linq; using Newtonsoft.Json; using ZAB_BLL; public class MoneyInfoHandler : IHttpHandler { pro proB = new pro(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; HttpRequest request = context.Request; HttpResponse response = context.Response; string callback = string.IsNullOrEmpty(request["callback"]) ? string.Empty : request["callback"]; string json; int clientId; int.TryParse(request["clientId"].Split('_')[1], out clientId); string types = request["types"].ToString(); string beginDate = Convert.ToDateTime(request["beginDate"]).ToShortDateString().ToString(); string endDate =Convert.ToDateTime(request["endDate"]).ToShortDateString().ToString(); DataTable dt = new DataTable(); try { if (clientId == 0) { throw new ArgumentException("clientId"); } string[] param = { "@userId?"+clientId, "@types?"+types, "@beginDate?"+beginDate, "@endDate?"+endDate, "@control?GL" }; //得到数据到datatable dt = proB.GetProcedureTable("pro_jetjxx", param, "pro_jetjxx"); } catch (Exception) { } //将数据转换到list中 List<MoneyInfo> list = DtConverToListM<MoneyInfo>.DtToList(dt); //将数据序列化到json json = JsonConvert.SerializeObject(list); //返回数据 response.Write(string.IsNullOrEmpty(callback) ? json : string.Format("{0}({1})", callback, json)); } public bool IsReusable { get { return false; } } } public class MoneyInfo { /// <summary> /// 类型 /// </summary> public string leixing{ get; set; } /// <summary> /// 金额 /// </summary> public string totMoney { get; set; } } #region DataTable convert to list public class DtConverToListM<T> where T : new() { public static List<T> DtToList(DataTable dt) { //定义集合 List<T> listCollection = new List<T>(dt.Rows.Count); //获得 T 模型类型 Type T_type = typeof(T); //获得 T 模型类型公共属性 PropertyInfo[] proper = T_type.GetProperties(); //临时变量,存储变量模型公共属性Name //遍历参数 DataTable的每行 foreach (DataRow dr in dt.Rows) { //实例化 T 模版类 T t = new T(); //遍历T 模版类各个属性 #region foreach (PropertyInfo p in proper) { //取出类属性之一 string tempname = p.Name; //判断DataTable中是否有此列 if (dt.Columns.Contains(tempname)) { //判断属性是否可写属性 if (!p.CanWrite) { continue; } //得到Datable单元格中的值 object value = dr[tempname]; //得到 T 属性类型 Type proType = p.PropertyType; //判断类型赋值 if (value != DBNull.Value) { // if (value.GetType() == proType) { p.SetValue(t, value, null); } else { if (proType == typeof(string)) { string temp = value.ToString(); p.SetValue(t, temp, null); } else if (proType == typeof(byte)) { byte temp = Convert.ToByte(value); p.SetValue(t, temp, null); } else if (proType == typeof(short)) { short temp = short.Parse(value.ToString()); p.SetValue(t, temp, null); } else if (proType == typeof(long)) { long temp = long.Parse(value.ToString()); p.SetValue(t, temp, null); } else if (proType == typeof(Int64)) { Int64 temp = Convert.ToInt64(value); p.SetValue(t, temp, null); } else if (proType == typeof(Int32)) { Int32 temp = Convert.ToInt32(value); p.SetValue(t, temp, null); } else if (proType == typeof(Int16)) { Int16 temp = Convert.ToInt16(value); p.SetValue(t, temp, null); } else { object temp = Convert.ChangeType(value, proType); p.SetValue(t, temp, null); } } } } } #endregion listCollection.Add(t); } return listCollection; } } #endregion