zoukankan      html  css  js  c++  java
  • .net通过一般处理程序模拟用户控件数据保持、Ispostback

    实现步骤:

    1.首先用StringBuiler将所需的表单等“画”出来,这里用StringBuider而不是直接用+号拼,这个问题不用解释吧呵呵

    2.通过将涉及到的文本框的数据动态的拼进去------实现用户控件数据保持

    3.在表单中添加一个隐藏域用来标示是否为回发过程

    应用一个简单的两个数相加求和的代码实现一下

    public class jsqHandler : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/html";
    
            //声明三个整形变量
            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());
            }
            //绘制表单,其中有个隐藏域用于判断是否是回发过程
            StringBuilder sbHtml = new StringBuilder();
            sbHtml.Append("<html><head><title>简单计算器</title></head><body><form method = 'post'>")
                .Append("<input type = 'text' name = 'txtNum1' value = '" + n1.ToString() + "'/>")
                .Append("+<input type = 'text' name = 'txtNum2' value = '" + n2.ToString() + "'/>")
                .Append("=<input type = 'text' name = 'txtResult' value = '" + result.ToString() + "'/>")
                .Append("<input type = 'hidden' name = 'IsPostBack' value = 'false'/>")
                .Append("<br/><input type = 'submit' value = '计算'/>")
                .Append("</form></body>");
            context.Response.Write(sbHtml);
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
  • 相关阅读:
    itertools mode 之 combinations用法
    Java排序方法及效率测试
    python数据处理——csv文件读取
    python数据处理——bsonId与时间戳的相互转换
    python图文识别案例
    python数据处理——字符串转数值类型
    ajax实现异步请求
    使用redis的evalsha功能实现获取数据时备份数据
    U盘写入保护去除(非物理性的)
    Git——.gitignore文件语法规范
  • 原文地址:https://www.cnblogs.com/wlitsoft/p/2459853.html
Copyright © 2011-2022 走看看