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

ASP.NET 一般处理程序显示用户信息列表功能的实现

时间:2016-11-28 08:06:49      阅读:210      评论:0      收藏:0      [点我收藏+]

标签: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 一般处理程序显示用户信息列表功能的实现

标签:work   als   pil   asp   returns   ash   font   add   deb   

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

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