美文网首页
ASP.NET(1)

ASP.NET(1)

作者: 山猪打不过家猪 | 来源:发表于2022-11-01 14:58 被阅读0次

1.认识asp.net

image.png
  • bookList.html 图书展示页
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="table.css">
</head>
<body>
    <div>
        <a href="AddBookInfo.html">添加书本 +</a>
        <a href="DelBookInfo.html">删除书本 -</a>
    </div>
    <table>
        <tr>
            <th>书名</th>
            <th>作者</th>
            <th>出版社ID</th>
            <th>出版时间</th>
            <th>详细</th>
        </tr>
        $tbody
    </table>
</body>
</html>
  • bookList.ashx图书展示页处理程序
public class bookList : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        string sql = "select bookName,Author,PublisherId,PublishDate from [Books]";
        string ConnStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(ConnStr))
        {
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            conn.Open();
            da.Fill(dt);
            StringBuilder sb = new StringBuilder();
            conn.Close();
            //使用DataTable
            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='showDetial.htlm'>详情</a></td></tr>",
                    dt.Rows[i]["bookName"],dt.Rows[i]["Author"],dt.Rows[i]["PublisherId"],Convert.ToDateTime(dt.Rows[i]["PublishDate"]).ToShortDateString()
                    
                    );
            }
            //1.匹配前端页面
            string filePath = context.Request.MapPath("bookList.html");
            //2.读取前端页面
            string fileContent = File.ReadAllText(filePath);
            //3.替换占位符
            fileContent = fileContent.Replace("$tbody", sb.ToString());
            //4.返回页面
            context.Response.Write(fileContent);
        }
    }

注意:详情页的传参是get方法,所以要这么写<td><a href='showDetial.htlm?id={4}'>详情</a>*

相关文章

网友评论

      本文标题:ASP.NET(1)

      本文链接:https://www.haomeiwen.com/subject/kbhjtdtx.html