A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

//HTML代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>计算器</title>
</head>
<body>
    <form action='' method='post'>
    <input type='text' name='txtNum1' value='{num1}' />
    +
    <input type='text' name='txtNum2' value='{num2}' />
    =
    <input type='text' name='txtSum' value='{res}' /><br />
    <input type='submit' value='计算' /><input type='hidden' name='hidIsPostBack' value='1' /></form>
</body>
</html>

//一般处理程序ashx代码
<%@ WebHandler Language="C#" Class="C07Cul" %>
using System;
using System.Web;
using System.IO;
public class C07Cul : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        //-----------------------------------------读取html模板内容------------------------------------------------------
        //根据虚拟路径获得物理路径
        string path = context.Server.MapPath("C07CulModel.html");
        string strHTML = File.ReadAllText(path);
        //--------------------------------获得浏览器提交的数据并运算----------------------------------------------
        string strNum1 = context.Request.Form["txtNum1"];
        string strNum2 = context.Request.Form["txtNum2"];
        int x = 0;
        int y = 0;
        int z = 0;
        //如果包含隐藏域的话,才执行相加运算操作
        if (!string.IsNullOrEmpty(context.Request.Form["hidIsPostBack"]))
        {
            if (!string.IsNullOrEmpty(strNum1) && !string.IsNullOrEmpty(strNum2))
            {
                if (int.TryParse(strNum1, out x) && int.TryParse(strNum2, out y))
                {
                    z = x + y;
                }
            }
        }
        //-----------------------------输出html代码到浏览器-------------------------------
        strHTML = strHTML.Replace("{num1}", x.ToString()).Replace("{num2}", y.ToString()).Replace("{res}", z.ToString());
        context.Response.Write(strHTML);
    }

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

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马