HTML Template page userlist.htm
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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>
<body>
<table>
<caption> User Information Sheet </caption>
<tr>
<th>EmpId</th>
<th>EmpName</th>
<th>EmpAge</th>
<th>DelFlag</th>
<th> Management </th>
</tr>
$tbody
</table>
</body>
General Handler Userlist.ashx Code
<%@ 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";
The first step connects the database
using (SqlConnection conn = new SqlConnection (GETCONNSTR ()))
{
Building a bridge in the second part
String Sql= "select * from Employee";
Using (SqlDataAdapter da =new SqlDataAdapter (Sql,conn))
{
The third step is to set up a DataTable to automatically open the connection and add the data to the memory table
DataTable dt = new DataTable ();
Da. Fill (DT);
Starts traversing the table and assembles the values into strings
StringBuilder SB =new StringBuilder ();
if (dt. ROWS.COUNT>0)//Determine if there is a record
{
for (int i=0;i<dt. ROWS.COUNT;++I)//traversal record
{
Sb. AppendFormat ("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3} </td><td><a href= ' showlist.ashx?id={0} ' > Detail </a>---Modify---delete </td><tr> ", dt. rows[i]["EmpId"],dt. rows[i]["EmpName"],dt. rows[i]["Empage"],dt. rows[i]["Delflag"]);
}
}
Read and get the template path
String Htmlpath=context. Server.MapPath ("userlist.htm");
Read template content
String Strhtml=file.readalltext (Htmlpath);
Replace template content with content in a string object
Strhtml=strhtml.replace ("$tbody", sb. ToString ());
Context. Response.Write (strhtml);
}
}
}
<summary>
Returns the connection string for the database
</summary>
<returns></returns>
public string Getconnstr ()
{
String connstr=configurationmanager.connectionstrings["ConnStr"]. ConnectionString;
return connstr;
}
public bool IsReusable {
get {
return false;
}
}
}
Database connection string configuration Web. config
<?xml version= "1.0"?>
<!--
For more information about how to configure an ASP. NET application, go to
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 generic handler displays the implementation of the User Information list feature