前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicCol.aspx.cs" Inherits="Content_DynamicCol" %>
后台 Data1.ashx 文件内容
<%@ WebHandler Language="C#" Class="data1" %> using System; using System.Web; using System.Data; using System.Collections.Generic; using System.Collections; using System.Configuration; public class data1 : IHttpHandler { RIFATM.DB.SqlHelper sqlh = new RIFATM.DB.SqlHelper(); public void ProcessRequest (HttpContext context) { context.Response.ContentType = "application/json"; DataTable dt = new DataTable(); DataTable dt2 = new DataTable(); DataTable resultdata = new DataTable(); string linkaddres = ""; string sql = ""; string sql2 = ""; if (context.Request.Params["action"] == "getcols1") { sql = "select * from Sys_home_set where orgcode='" + context.Request.Params["orgcode"].ToString() + "' and id='7' order by descid asc"; sql2 = "select sql from Sys_home where orgcode='" + context.Request.Params["orgcode"].ToString() + "' and id='7'"; } dt = sqlh.GetTable(sql); dt2 = sqlh.GetTable(sql2); resultdata = sqlh.GetTable(dt2.Rows[0]["sql"].ToString()); //以下是核心部分 IList rowObjects = new List(); ArrayList ColNs = new ArrayList(); IList ColMs = new List(); ColNs.Add(""); var colm = new { name = "img", index = "img", width = 32, sortable = false }; ColMs.Add(colm); ColNs.Add(""); var colmog = new { name = "OgCode", index = "OgCode", hidden = true }; ColMs.Add(colmog); Type homecols = ClassHelper.BuildType("homecols"); List cpo = new List(); ClassHelper.CustPropertyInfo cpi; foreach (DataRow dr in dt.Rows) { ColNs.Add(dr["ColumnText"].ToString()); var colms = new { name = dr["ShowColumn"].ToString(), index = dr["ShowColumn"].ToString(), width = 100 }; if (dr["linkaddress"].ToString().Length != 0) { linkaddres = dr["linkaddress"].ToString(); } cpi=new ClassHelper.CustPropertyInfo("System.String",dr["ShowColumn"].ToString()); cpo.Add(cpi); ColMs.Add(colms); } ///图标和业务组编码 if (context.Request.Params["ico"] == "true") { cpi = new ClassHelper.CustPropertyInfo("System.String", "img"); cpo.Add(cpi); } if (context.Request.Params["og"] == "true") { cpi = new ClassHelper.CustPropertyInfo("System.String", "OgCode"); cpo.Add(cpi); homecols = ClassHelper.AddProperty(homecols, cpo); } /// homecols=ClassHelper.AddProperty(homecols,cpo); IList oo = new List { ClassHelper.CreateInstance(homecols) }; foreach (DataRow dr in resultdata.Rows) { object o = ClassHelper.CreateInstance(homecols); foreach (ClassHelper.CustPropertyInfo _cpo in cpo) { string colname = _cpo.FieldName; if (string.Compare(colname,"img") == 0) { ClassHelper.SetPropertyValue(o, "img", " "); } if (string.Compare(colname, "OgCode") == 0) { ClassHelper.SetPropertyValue(o, "OgCode", "RFHG16000"); } if ((string.Compare(colname, "img") != 0) && (string.Compare(colname, "OgCode") != 0)) { ClassHelper.SetPropertyValue(o, colname, dr[_cpo.FieldName.Trim()].ToString()); } } oo.Add(o); } oo.RemoveAt(0); //不知道为什么生成的第一行数据是NULL,删除空行数据 var resultObj = new { ColNs = ColNs, // 总页数 ColMs = ColMs, rows = oo }; context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(resultObj)); } public bool IsReusable { get { return false; } }
}