ASP.NET 使用ashx文件动态生成表格
引自百度百科:
.ashx文件是用于写web handler的。.ashx文件与.aspx文件类似,它免去了普通的.aspx页面的控件解析以及页面处理的过程。其实就是带HTML和C#的混合文件。.ashx文件适合产生供浏览器处理、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。
下面分享一下使用ashx文件动态生成表格的例子。
首先在需要动态生成表格的aspx文件(这里称之为“A.aspx”)里放入一个Div,用作生成表格的载体。例如:
<div id="divTable">
</div>
在A.aspx页面中加入js代码:
<script type="text/javascript"> /*********************/ /*****创建xmlHttp*****/ /*********************/ function CreateXmlHttp() { var xmlHttp; // Internet Explorer try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("您的浏览器不支持AJAX!"); return false; } } return xmlHttp; } window.onload = function () { //创建xmlhttp var ajaxValue = ""; var xmlHttp = CreateXmlHttp(); var divHtml = document.getElementById("divTable"); if (xmlHttp) { xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4) { divHtml.innerHTML = xmlHttp.responseText; } else { divHtml.innerHTML = "正在加载,请稍后..."; } } var companyID = document.getElementById("hidCompanyID").value;
var jobTypeCode2 = document.getElementById("hidJobTypeCode2").value;
var url = "/HRManager/RenCaiKu/DetailDataTable.ashx" + "?CompanyID=" + companyID + "&JobTypeCode2=" + jobTypeCode2;
xmlHttp.open("GET", url, true);
xmlHttp.send();
} if (ajaxValue.length > 0) { alert("show message"); return false; } } </script>
可以将参数传入处理文件DetailDataTable.ashx中。DetailDataTable.ashx文件实现如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Text; 6 using System.Web.SessionState; 7 using System.Data; 8 9 namespace HRZP2.HRManager.RenCaiKu 10 { 11 /// <summary> 12 /// DetailDataTable 的摘要说明 13 /// </summary> 14 public class DetailDataTable : IHttpHandler, IRequiresSessionState 15 { 16 private string _companyID = string.Empty; 17 public string CompanyID 18 { 19 get { return _companyID; } 20 set { _companyID = value; } 21 } 22 23 private string _jobTypeCode2 = string.Empty; 24 public string JobTypeCode2 25 { 26 get { return _jobTypeCode2; } 27 set { _jobTypeCode2 = value; } 28 } 29 30 public void ProcessRequest(HttpContext context) 31 { 32 //清空缓存 33 context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 34 context.Response.ContentType = "text/plain"; 35 36 CompanyID = context.Request.QueryString["CompanyID"].ToString(); 37 JobTypeCode2 = context.Request.QueryString["JobTypeCode2"].ToString(); 38 39 string strResult = string.Empty; 40 strResult = CreateDetailTable(Convert.ToInt32(CompanyID), Convert.ToInt32(JobTypeCode2)); 41 context.Response.Write(strResult); 42 } 43 44 45 private string CreateDetailTable(int companyID, int jobTypeCode2) 46 { 47 HRS.BLL.Position.AP_SUBGRP bllAPS = new HRS.BLL.Position.AP_SUBGRP(); 48 HRS.BLL.Resume.ResumeStoreStatistic bllRSS = new HRS.BLL.Resume.ResumeStoreStatistic(); 49 DataTable positionTable = bllAPS.GetSecondRootbyParentId(jobTypeCode2); 50 List<DataTable> dataList = new List<DataTable>(); 51 StringBuilder sbTable = new StringBuilder(); 52 //创建表格 53 if (positionTable.Rows.Count > 0) 54 { 55 sbTable.Append(string.Format("<table class='{0}' style='width:100%;' cellPadding='0' cellSpacing='0'>", GetTableCss())); 56 //创建第一行表头 57 sbTable.Append("<tr>"); 58 foreach (DataRow row in positionTable.Rows) 59 { 60 sbTable.Append(string.Format("<td class='{0}' style='text-align:center'>{1}</td>", GetHeaderCss(), row["JobTypeName3"])); 61 int jobTypeCode3 = Convert.ToInt32(row["JobTypeCode3"].ToString()); 62 DataTable data = bllRSS.StatisticDetail(companyID, jobTypeCode2, jobTypeCode3); 63 dataList.Add(data); 64 } 65 sbTable.Append("</tr>"); 66 //得到DataTable链表中单个Table最大的行数 67 int maxRowsCount = GetMaxRowsCount(dataList); 68 int columnCount = dataList.Count; 69 string strRowCss = string.Empty; 70 string strCellCss = string.Empty; 71 for (int i = 0; i < maxRowsCount; i++) 72 { 73 //设置样式 74 if (i % 2 == 0) 75 { 76 strRowCss = GetRowCss(); 77 strCellCss = GetCellCss(); 78 } 79 else 80 { 81 strRowCss = GetAltRowCss(); 82 strCellCss = GetAltCellCss(); 83 } 84 85 sbTable.Append("<tr class='" + strRowCss + "'>"); 86 for (int j = 0; j < columnCount; j++) 87 { 88 //当前行数没有超过该DataTable的最大行数 89 if (i < dataList[j].Rows.Count) 90 { 91 if (dataList[j].Rows[i]["ResumeCount"].ToString() != "0") 92 { 93 string urlStr = "ResumeSearchList.aspx?Type=Company&CompanyID=" + companyID + "&JOB_ID=" + dataList[j].Rows[i]["JOB_ID"].ToString(); 94 sbTable.Append(string.Format( 95 "<td class='{0}'>{1}<div style='float: right; width: 10%; font-weight: bold'><a href='{2}'>{3}</a></div></td>", 96 strCellCss, dataList[j].Rows[i]["NAME"], urlStr ,dataList[j].Rows[i]["ResumeCount"])); 97 } 98 else 99 { 100 sbTable.Append(string.Format( 101 "<td class='{0}'>{1}</td>",strCellCss, dataList[j].Rows[i]["NAME"])); 102 } 103 } 104 else 105 { 106 sbTable.Append(string.Format("<td class='{0}'></td>", strCellCss)); 107 } 108 } 109 sbTable.Append("</tr>"); 110 } 111 sbTable.Append("</table>"); 112 } 113 else 114 { 115 sbTable.Append("<table><tr><td>无记录</td></tr></table>"); 116 } 117 return sbTable.ToString(); 118 } 119 120 121 /// <summary> 122 /// 得到DataTable链表中单个Table最大的行数 123 /// </summary> 124 /// <param name="dataList"></param> 125 /// <returns></returns> 126 private int GetMaxRowsCount(List<DataTable> dataList) 127 { 128 int maxRowsCount = 0; 129 if (dataList.Count > 0) 130 { 131 maxRowsCount = dataList[0].Rows.Count; 132 133 foreach (DataTable data in dataList) 134 { 135 if (data.Rows.Count > maxRowsCount) 136 { 137 maxRowsCount = data.Rows.Count; 138 } 139 } 140 } 141 return maxRowsCount; 142 } 143 144 #region 获取样式 145 /// <summary> 146 /// 表格样式 147 /// </summary> 148 /// <returns></returns> 149 private string GetTableCss() 150 { 151 return "RadGrid_TableDefault"; 152 } 153 154 /// <summary> 155 /// 行样式 156 /// </summary> 157 /// <returns></returns> 158 private string GetRowCss() 159 { 160 return "RadGrid_Row"; 161 } 162 163 /// <summary> 164 /// 行样式 165 /// </summary> 166 /// <returns></returns> 167 private string GetAltRowCss() 168 { 169 return "RadGrid_AltRow"; 170 } 171 172 /// <summary> 173 /// 表头单元格样式 174 /// </summary> 175 /// <returns></returns> 176 private string GetHeaderCss() 177 { 178 return "RadGrid_Header"; 179 } 180 181 /// <summary> 182 /// 单元格样式 183 /// </summary> 184 /// <returns></returns> 185 private string GetCellCss() 186 { 187 return "RadGrid_Cell"; 188 } 189 190 /// <summary> 191 /// 单元格样式 192 /// </summary> 193 /// <returns></returns> 194 private string GetAltCellCss() 195 { 196 return "RadGrid_AltCell"; 197 } 198 #endregion 199 200 public bool IsReusable 201 { 202 get 203 { 204 return false; 205 } 206 } 207 } 208 }
使用ashx处理网页的动态生成文本、表格可读性高、便于维护。