zoukankan      html  css  js  c++  java
  • .net通过一般处理程序模拟用户控件数据保持、Ispostback 【第二版将html与ashx文件分开】

    直接上代码:

    HTML代码,其中表单中input标签中的value值首先用一个占位符占位,然后在ashx中将其替换

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
    <form action = "02jsqHandler.ashx" method = "post">
        <input type = "text" name = "txtNum1" value = "@txtNum1" />+
        <input type = "text" name = "txtNum2" value = "@txtNum2" />
        =<input type = "text" name = "txtResult" value = "@txtResult" />
        <input type = "hidden" name = "IsPostBack" value = "false" />
        <input type = "submit"  value = "计算" />
    </form>
    </body>
    </html>

    ashx代码:

    首先用io中的File.ReadAllText()方法将html文件读出来

    public class _02jsqHandler : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/html";
            //获得html文件的内容
            string path = context.Server.MapPath("03jsq2.htm");
            string strHtml = File.ReadAllText(path);
            //声明三个整形变量
            int n1 = 0, n2 = 0, result = 0;
            bool ispostback = Convert.ToBoolean(context.Request.Form["IsPostBack"]);
            if (!ispostback)  //判断是否是第一次加载相当于aspx.cs中的IsPostBack
            {
                //分别获得数1和数2
                string sn1 = context.Request.Form["txtNum1"];
                string sn2 = context.Request.Form["txtNum2"];
                if (int.TryParse(sn1, out n1) && int.TryParse(sn2, out n2))
                {
                    result = n1 + n2;
                }
                //context.Response.Write(result.ToString());
            }
            //将html中的@参数(占位符替换为对应的数据)
            strHtml = strHtml.Replace("@txtNum1", n1.ToString())
                .Replace("@txtNum2", n2.ToString())
                .Replace("@txtResult", result.ToString());
            //将html写回页面
            context.Response.Write(strHtml);
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
  • 相关阅读:
    bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
    bzoj 2157: 旅游 (LCT 边权)
    bzoj 3669: [Noi2014]魔法森林 (LCT)
    bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
    bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
    bzoj 3282: Tree (Link Cut Tree)
    模拟赛1
    Some tricks
    模拟赛简要题解与心得
    Noip 训练指南
  • 原文地址:https://www.cnblogs.com/wlitsoft/p/2460603.html
Copyright © 2011-2022 走看看