实现步骤:

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;
        }
    }

 

 posted on 2012-04-20 21:19  WlitSoft  阅读(598)  评论(0编辑  收藏  举报