例子: 使用hidden实现点击提交按钮数字加1
数值自增.htm
复制代码代码如下:
<form action="数值自增.ashx" method="post">
<input type="hidden" name="_viewstate" value="a" />
<input type="hidden" name="_div" value="@n" />
<!-- <input name="txt" type="text" value="@value" />-->
<div>@n</div>
<input type="submit" value="click" />
</form>
使用一般处理程序实现
数值自增.ashx
复制代码代码如下:
int n = 0;
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string path = context.Request.MapPath("数值自增.htm");
string html = System.IO.File.ReadAllText(path);
//判断页面是否是第一次加载
string viewstate = context.Request.Form["_viewstate"];
if (!string.IsNullOrEmpty(viewstate))
{
//点击按钮 post
//获取隐藏域的值
string s = context.Request.Form["_div"];
if (int.TryParse(s, out n))
{
n++;
html = html.Replace("@n",n.ToString());
}
}
else
{
//页面首次加载,给div和div对应的隐藏域赋值
html = html.Replace("@n", n.ToString());
}
context.Response.Write(html);
}