Show.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>
<td>EmpId</td>
<td>$EmpId</td>
</tr>
<tr>
<td>EmpName</td>
<td>$EmpName</td>
</tr>
<tr>
<td>EmpAge</td>
<td>$EmpAge</td>
</tr>
<tr>
<td>DelFlag</td>
<td>$DelFlag</td>
</tr>
</table>
</body>
</html>
show.ashx文件代码如下
<%@ WebHandler Language="C#" Class="Show" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
public class Show : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
int id;
if (int.TryParse(context.Request.QueryString["id"], out id))
{
using (SqlConnection conn = new SqlConnection(GetConnStr()))
{
using (SqlDataAdapter apter = new SqlDataAdapter("SELECT * FROM Employee WHERE [email protected]",conn))
{
SqlParameter par = new SqlParameter("@id",SqlDbType.Int);
par.Value = id;
apter.SelectCommand.Parameters.Add(par);
DataTable ds = new DataTable();
apter.Fill(ds);
if (ds.Rows.Count > 0)
{
string htmlPath = context.Server.MapPath("Show.htm");
string htmlContent = File.ReadAllText(htmlPath);
htmlContent = htmlContent.Replace("$EmpId", ds.Rows[0]["EmpId"].ToString()).Replace("$EmpName", ds.Rows[0]["EmpName"].ToString()).Replace("$EmpAge", ds.Rows[0]["EmpAge"].ToString()).Replace("$DelFlag", ds.Rows[0]["DelFlag"].ToString());
context.Response.Write(htmlContent);
}
}
}
}
else
{
context.Response.Write("参数错误");
}
}
/// <summary>
/// 返回数据库的连接字符串
/// </summary>
/// <returns></returns>
public string GetConnStr()
{
string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
return ConnStr;
}
public bool IsReusable {
get {
return false;
}
}
}