01List.ashx(班级列表动态页面)

01List.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        #tbList {
            border:1px solid #0094ff;
            border-collapse:collapse;
            width:500px;
            margin:50px auto;
        }
        #tbList th,td{
            border:1px solid #0094ff;
            padding:5px;
        }
    </style>
    <script type="text/javascript">
        function doDel(id)
        {
            if (confirm("您确定要删除吗?"))
            {
                window.location = "03Del.ashx?id=" + id;
            }
        }
    </script>
</head>
<body>
    <a href="04Add.html">新增</a>
    <table id="tbList">
        <tr>
            <th>Id</th>
            <th>名称</th>
            <th>人数</th>
            <th>操作</th>
        </tr>
        <!--用户自定义占位符-->
        @{表格内容}
    </table>
</body>
</html>

01List.ashx

using System;
using System.Collections.Generic;
using System.Data;
using System.Web;

namespace AspNetAshx
{
    /// <summary>
    /// 班级列表动态页面
    /// </summary>
    public class _01List : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/html";
            //1.读取模版数据
            string strHtml = CommonHelper.GetFileContent("01List.html");
            //2.读取数据库
            DataTable dt = SqlHelper.GetTable("select * from Classes where CIsDel = 0");
            System.Text.StringBuilder sbHtml = new System.Text.StringBuilder(1000);
            //3.生成 表格行字符串
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sbHtml.Append("<tr>");
                    sbHtml.Append("<td>");
                    sbHtml.Append(dr["cId"]);
                    sbHtml.Append("</td>");
                    sbHtml.Append("<td>");
                    sbHtml.Append("<a href='07GetClassDetail.ashx?id=" + dr["cId"] + "'>" + dr["cName"] + "</a>");
                    sbHtml.Append( dr["cName"] );
                    sbHtml.Append("</td>");
                    sbHtml.Append("<td>");
                    sbHtml.Append(dr["cCount"]);
                    sbHtml.Append("</td>");
                    sbHtml.Append("<td>");
                    sbHtml.Append("<a href='javascript:doDel("+dr["cid"]+")'>删</a> <a href='02Modify.ashx?id="+dr["cid"]+"'>改</a>");
                    sbHtml.Append("</td>");
                    sbHtml.Append("</tr>");
                }
            }
            //4.将 表格行字符串 替换到 模版占位符 处
            strHtml = strHtml.Replace("@{表格内容}", sbHtml.ToString());
            //5.将替换后的模版字符串 发给 浏览器
            context.Response.Write(strHtml);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

posted @ 2016-01-06 15:36  狼牙者.net  阅读(391)  评论(0编辑  收藏  举报