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;
            }
        }
  • 相关阅读:
    洛谷 P1260 工程规划(差分约束)
    洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)
    [模板]单调队列
    [模板]LIS(最长上升子序列)
    洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
    如何求数字n的因数个数及因数和
    [模板]tarjan缩点+拓扑排序
    itext生成pdf(附带页眉,页脚,页码)
    工作总结03
    工作总结02(海报上传模块)
  • 原文地址:https://www.cnblogs.com/wlitsoft/p/2459853.html
Copyright © 2011-2022 走看看