.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'/>")
相关新闻>>
- 发表评论
-
- 最新评论 进入详细评论页>>
今日头条
更多>>您可能感兴趣的文章
- 向Excel文档中嵌入VBA控件和UserForm并显示
- ASP.net页面中请求远程Web站点
- 使用ASP.NET MVC3+EF+Jquery制作文字直播系统(一
- ASP.NET之Gridview图解(1)
- ASP.NET FormsAuthentication跨站点登录时绝对地址返
- Request.Cookies 和 Response.Cookies 的区别
- 步步为营 SharePoint 开发学习笔记系列&nb
- 用OpenXml在文档的尾部添加一个Rich Text Content Con
- ASP.NET生成高质量缩略图通用函数(c#代码)
- Spring.Net学习系列一: 统一异常处理



