无状态Httpupeng容权不遇配华登租
Http协议是无状态的,不会记得上次和网页“发生了什么”。
试验:
private 字段十+,然后输出到页面上,然后重复刷新页面。服务器不记的上次给了浏览器什么,否则服务器的压力会太大。
每次请求到达asp.net,都创建一个HttpHandler的新对象来处理。。
浏览系需要记住这些值,下次再提交服务器的射候就要把上次的值提交给服务器,让他想起来。如果要知道上一次的状态,一个方法是在对浏览器响应结束之前将状态信息保存到页面表单中,下次页面再向服务器发出请求的射候带上这些状态信息,这样服务器就能根据这些状态信息还原上次的状态了,类似于去看病的病历本。
对type='text”自增;对于span自增。。input宽度自增,尽量不要这么干,js做更好。
一个案例验证:
MomeryTest.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form action="MomeryTest.ashx" method="post">
<input type="text" name="number" value="{number}" />
<input type="submit" name="btn1" />
<!--这里可以用来控制一个层的手动增长-->
<input type="text" style=" width:{number}0px;border-color:blue;" />
</form>
</body>
</html>
MomeryTest.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web1.Day3;
namespace Web1.Day4
{
/// <summary>
/// 实现点击数字自动增长的记忆功能!!!
/// </summary>
public class MomeryTest : IHttpHandler
{
//private int i;//该字段默认初始化时0;在这里只是做一下说明
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//1、引入CommonHelper这个类的命名空间
//2、读取html模板页
string html = CommonHelper.ReadHtml("~/Day4/MomeryTest.html");
//3、查看请求的报文,如果请求的报文是第一次加载的页面,,
// 报文中是没有name="btn1"的,因为第一次加载的时候,你没有点击这个”提交“按钮
// 所以可以以这个特点进行数值的赋值的判断
if (string.IsNullOrEmpty(context.Request["btn1"]))
{
//4、页面刚加载的情况
//5、替换指定的字符串
html = html.Replace("{number}", "0");
//i = 0;
}
else
{
//i++;
int num=Convert.ToInt32( context.Request["number"]);
num++;
//i=num;
// html = html.Replace("{number}", i.ToString());
html = html.Replace("{number}", num.ToString());
}
context.Response.Write(html);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}