HTML模板页
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <title>@title</title> 6 <style type="text/css"> 7 table 8 { 9 border:1px red; 10 } 11 </style> 12 </head> 13 <body> 14 @body 15 </body> 16 </html>
List.ashx页面
1 <%@ WebHandler Language="C#" Class="List" %> 2 3 using System; 4 using System.Web; 5 6 7 using System.Data; 8 using System.Data.SqlClient; 9 using System.Text; 10 using System.IO; 11 12 public class List : IHttpHandler { 13 14 public void ProcessRequest (HttpContext context) { 15 //context.Response.ContentType = "text/plain"; 16 //context.Response.Write("Hello World"); 17 DataTable dt = LoadData(); 18 StringBuilder sb = new StringBuilder("<table style = \"border:1px\">"); 19 foreach(DataRow dr in dt.Rows) 20 { 21 sb.Append("<tr>"); 22 sb.Append("<td>"+ dr[0]+"</td>"); 23 sb.Append("<td>"+ dr[1]+"</td>"); 24 sb.Append("<td>"+ dr[2]+"</td>"); 25 sb.Append("</tr>"); 26 } 27 sb.Append("</table>"); 28 string strPath = context.Server.MapPath("./HtmlModel.htm"); 29 string strHtml = File.ReadAllText(strPath); 30 strHtml= strHtml.Replace("@title","欢迎查看学生列表页"); 31 strHtml= strHtml.Replace("@body", sb.ToString()); 32 context.Response.Write(strHtml); 33 } 34 35 private DataTable LoadData() 36 { 37 DataTable dt = null; 38 try 39 { 40 SqlConnection conn = new SqlConnection("Data Source=PC--20130405SCI\\YAOSIR;Initial Catalog=Clients;User ID= sa;Password=123456"); 41 42 conn.Open(); 43 SqlCommand cmd = new SqlCommand(); 44 45 cmd.CommandText = "Select * from OrderClient"; 46 47 cmd.Connection = conn; 48 SqlDataReader dr = cmd.ExecuteReader(); 49 //SqlDataAdapter da = new SqlDataAdapter(selectStr,conn); 50 dt = new DataTable(); 51 dt.Load(dr); 52 53 54 //string sqlStr = "Select * from OrderClient"; 55 //SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn); 56 //dt = new DataTable(); 57 //da.Fill(dt); 58 } 59 catch (Exception e ) 60 { 61 62 throw new Exception("执行出现异常..."); 63 } 64 65 return dt; 66 } 67 68 public bool IsReusable { 69 get { 70 return false; 71 } 72 } 73 74 }