码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET 一般处理程序展示详细页面

时间:2016-11-28 09:08:59      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:getc   html   ctc   style   elf   connect   turn   msu   center   

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 EmpId=@id",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;
}
}

}

ASP.NET 一般处理程序展示详细页面

标签:getc   html   ctc   style   elf   connect   turn   msu   center   

原文地址:http://www.cnblogs.com/zhaodachao/p/6108036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!