HTML模板页
1 2 3 4 5 @title 6 12 13 14 @body15 16
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("
");19 foreach(DataRow dr in dt.Rows)20 {21 sb.Append(" ");22 sb.Append(" "+ dr[0]+" | ");23 sb.Append(" "+ dr[1]+" | ");24 sb.Append(" "+ dr[2]+" | ");25 sb.Append("
");26 }27 sb.Append("
");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 try39 {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 }