一、新建一个网站,在网站下面添加一个一般处理程序DemoHandler.ashx,代码如下:
<%@ WebHandler Language="C#" Class="DemoHandler" %>using System;using System.Web;using System.Data.SqlClient;using System.Configuration;using System.Text;public class DemoHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/html"; //context.Response.WriteFile("1.jpg"); StringBuilder sb = new StringBuilder();//拼接字符串用 sb.Append(""); #region 拼接table //拼接表头 表头要用标签 sb.Append("
Id | UserNumber | PassWord | ZhanMing | 操作 |
---|---|---|---|---|
{0} | {1} | {2} | {3} | 详情 |
二、打开web.config,在里面将连接数据库字符串 写入:
当点击 操作的“详情”时,跳转到ShowDetil.ashx页面
代码如下:
<%@ WebHandler Language="C#" Class="ShowDetil" %>using System;using System.Web;using System.Text;using System.Configuration;using System.Data.SqlClient;using System.Data;using System.IO;public class ShowDetil : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/html"; //context.Response.Write("Hello World"); StringBuilder sb = new StringBuilder();//拼接字符串 //需要拿到当前的ID string id= context.Request.QueryString["id"]; int qid = int.Parse(id);//把他转换为整形 //下面链接数据库来获取ID string strLink = ConfigurationManager.ConnectionStrings["Pwd"].ConnectionString;//存放连接字符串 string sql = "select *from 用户名和密码 where id=@id "; using (SqlDataAdapter adapter=new SqlDataAdapter (sql,strLink)) { //传递参数 adapter.SelectCommand.Parameters.Add("@id", qid); //在该网页中填充一个表格 DataTable dt = new DataTable(); adapter.Fill(dt); //下面需要拼接表格了 我们可以写在一个html模板中,所以可以新建一个HTML模板 sb.AppendFormat("ID:{0}", dt.Rows[0]["Id"]);//id信息 sb.AppendFormat("UserName:{0}", dt.Rows[0]["UserName"]); sb.AppendFormat("PassWord:{0}", dt.Rows[0]["PassWord"]); sb.AppendFormat("ZhanMing:{0}", dt.Rows[0]["zhanming"]); } //把HTML模板读取出来,然后替换数据占位符 //把相对于网站根目录的路径转成磁盘上的绝对路径 string path= context.Request.MapPath("/ShowDetailTemp.html"); string textTemp= File.ReadAllText(path);//将整个HTML模板读出来赋给textTemp string result= textTemp.Replace(" @StrTrBody", sb.ToString()); context.Response.Write(result); } public bool IsReusable { get { return false; } }}
当鼠标点击 详情 时,显示的页面可以写在一个html 模板中,代码如下: