標籤:work als pil asp returns ash font add deb
HTML模版頁面UserList.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
body{font-family:"Microsoft YaHei","SimSun"}
table{border-collapse:collapse; border:1px sold #000;width:80%;margin:0px auto;text-align:center;}
tr,td,th{border:1px solid #000}
th{font-weight:bold;background:#00F;color:#FFF;line-height:30px;}
td{line-height:30px;}
caption{font-size:48px;font-weight:bold;padding-bottom:20px;}
</style>
</head>
<body>
<table>
<caption>使用者資訊表</caption>
<tr>
<th>EmpId</th>
<th>EmpName</th>
<th>EmpAge</th>
<th>DelFlag</th>
<th>管理</th>
</tr>
$tbody
</table>
</body>
</html>
一般處理常式UserList.ashx代碼
<%@ WebHandler Language="C#" Class="UserList" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.IO;
public class UserList : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
//第一步串連資料庫
using(SqlConnection conn = new SqlConnection(GetConnStr()))
{
//第二部建立橋樑
string sql="SELECT * FROM Employee";
using(SqlDataAdapter da =new SqlDataAdapter(sql,conn))
{
//第三步建立DataTable自動開啟串連並且把資料添加到記憶體表中
DataTable dt = new DataTable();
da.Fill(dt);
//開始遍曆表,並且把值組裝成字串
StringBuilder sb =new StringBuilder();
if(dt.Rows.Count>0)//判斷是否有記錄
{
for(int i=0;i<dt.Rows.Count;++i)//遍曆記錄
{
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td><a href=‘ShowList.ashx?id={0}‘>詳細</a>---修改---刪除</td><tr>",dt.Rows[i]["EmpId"],dt.Rows[i]["EmpName"],dt.Rows[i]["EmpAge"],dt.Rows[i]["DelFlag"]);
}
}
//讀取並擷取模版路徑
string htmlPath=context.Server.MapPath("UserList.htm");
//讀模數版內容
string strHtml=File.ReadAllText(htmlPath);
//將模版內容替換成字串對象中的內容
strHtml=strHtml.Replace("$tbody",sb.ToString());
context.Response.Write(strHtml);
}
}
}
/// <summary>
/// 返回資料庫的連接字串
/// </summary>
/// <returns></returns>
public string GetConnStr()
{
string ConnStr=ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
return ConnStr;
}
public bool IsReusable {
get {
return false;
}
}
}
資料庫連接字串配置web.config
<?xml version="1.0"?>
<!--
有關如何配置 ASP.NET 應用程式的詳細資料,請訪問
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="ConnStr" connectionString="server=.;uid=sa;pwd=sa;database=Alex_blog"/>
</connectionStrings>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
</configuration>
ASP.NET 一般處理常式顯示使用者資訊列表功能的實現