迎接2012之页面静态化

 一、代码

(1)和上篇博客差不多,只需要修改几个地方,增加几个页面。

(2)HTMLPage.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</title>
</head>
<body>
@Body
</body>
</html>

(3)Add.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Add.aspx.cs" Inherits="Add" %>

<!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>
     
     <script type="text/javascript">
         $(document).ready(function () {
             $("#Submit1").click(function () {
                 if ($("#Text1").val() == "") {
                     alert("请输入用户名");
                     return false;
                 }
                 if ($("#Password1").val() == "") {
                     alert("请输入密码");
                     return false;
                 }
                 if ($("#Select1").val() == "-1") {
                     alert("请选择性别");
                     return false;
                 }
                 $.ajax({
                     type: "post",
                     url: "AddStaticPage.ashx",
                     data: "username=" + $("#Text1").val() + "&password=" + $("#Password1").val() + "&sex=" + $("#Select1").val() + "&time=" + function () {
                         var dt = new Date();
                         var dts = dt.getYear().toString() + dt.getMonth().toString() + dt.getDay().toString() + dt.getHours().toString() + dt.getMinutes().toString() + dt.getSeconds().toString() + dt.getMilliseconds().toString();
                         return dts;
                     } (),
                     success: function (dt) {
                         alert(dt);
                         $("#Submit1").attr("value", "提交").removeAttr("disabled");
                         $("#Text1").val("");
                         $("#Password1").val("");
                         $("#Select1").val("-1");
                     },
                     Error: function (dt) {
                         alert("出错啦!");
                     },
                     beforeSend: function (dt) {
                         $("#Submit1").attr({ "value": "正在提交", "disabled": "disabled" });
                     }

                 });
                 return false;
             })
         });
     </script>
</head>
<body>
    <form id="form1" action="">
    <div>
        用户名:<input id="Text1" type="text" name="username" />
        <br />
        密码:<input id="Password1" type="password" name="password" />
        <br />
        性别:
        <select id="Select1" name="sex">
            <option value="-1">请选择</option>
            <option value="0">男</option>
            <option value="1">女</option>
        </select>
        <br />
        <input id="Submit1" type="submit" value="提交" /><input id="Reset1" type="reset"
            value="清除" />
    </div>
    </form>
</body>
</html>

(3)AddStaticPage.ashx

<%@ WebHandler Language="C#" Class="AddStaticPage" %>

using System;
using System.Web;

public class AddStaticPage : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string username = context.Request.Form["username"].ToString();
        string password = context.Request.Form["password"].ToString();
        string sex = context.Request.Form["sex"].ToString();
        ThreeLevelBLL.Users user = new ThreeLevelBLL.Users();
        int i = user.UserInsert(new ThreeLevelMODEL.Users(0, username, password, (sex == "0" ? true : false)));
        if (i > 0)
        {
            string htmlstr = System.IO.File.ReadAllText(context.Server.MapPath("~") + "\\HTMLPage.htm", System.Text.Encoding.UTF8);
            htmlstr = htmlstr.Replace("@Title", username + "的信息").Replace("@Body", "用户名:" + username + "<br/>密码:" + password + "<br/>性别:" + (sex == "0" ? "男" : "女"));
            System.IO.File.WriteAllText(context.Server.MapPath("~") + "\\" + context.Request.Form["time"].ToString() + ".htm", htmlstr, System.Text.Encoding.UTF8);
            context.Response.Write("添加成功");
        }
        else
        {
            context.Response.Write("添加失败");
        }
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

二、效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值