ASP.Net学习笔记010--加法计算器

/WebSiteTest中新建
AddCalc1.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>
</head>
<body>
<form action="AddCalc1.ashx">
<input type="hidden" name="huilaile" value="yes" />
<input type="text" name="num1" value="@num1" />+<input type="text" name="num2" value="@num2" /><input type="submit" value="=" />
<input type="text" value="@result" readonly="readonly" />
</form>
</body>
</html>
-------------------------------------------------------
<%@ WebHandler Language="C#" Class="AddCalc1" %>


using System;
using System.Web;


public class AddCalc1 : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        string huilaila = context.Request["huilaile"];
     //   context.Response.Write("Hello World");
        //如果想保存,前两个加数的值,需要添加变量
        string num1 = "";
        string num2 = "";
            
   
        string result = "";
        if (huilaila=="yes") {
              num1=context.Request["num1"];
              num2=context.Request["num2"];
             result = Convert.ToString (Convert.ToInt32(num1) + Convert.ToInt32(num2));
            context.Response.Write("<script type='text/javascript'>alert('Hello everyOne Im Credream')</script>");
        }
        string html文件模板全路径 = context.Server.MapPath("AddCalc1.htm");//得到文件的全路径
        string content = System.IO.File.ReadAllText(html文件模板全路径);
        //替换前两个单元格的值,保存原有的值
        //这里每写一次:content就是一个html页面,但是由于浏览器的容错性,所以不会显示出错
       // context.Response.Write(content);
        //这里的context.Response.Write(content);,可以看出来每一步,它是如何替换的
        content = content.Replace("@num1", num1);
       // context.Response.Write(content);
        content = content.Replace("@num2", num2);
      //  context.Response.Write(content);
        content = content.Replace("@result", result);
        context.Response.Write(content);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }


}
-------------------------------
生成的下面的html代码很明显是错误的,但是显示出来并没有问题,提现了浏览器的容错性:
先看看浏览器的容错性:


<script type='text/javascript'>alert('Hello everyOne Im Credream')</script><!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>
</head>
<body>
<form action="AddCalc1.ashx">
<input type="hidden" name="huilaile" value="yes" />
<input type="text" name="num1" value="1" />+<input type="text" name="num2" value="1" /><input type="submit" value="=" />
<input type="text" value="2" readonly="readonly" />
</form>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

添柴程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值