新建项目:
/WebSiteTest
IncValue2.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="IncValue2.ashx">
<input type="hidden" name="ispostback" value ="true" />
<!--<div name="num1">@value</div> 这里给@value设置name属性,在后台是取不到的,因为
只有,input ,textarea,select控件的值才会被提交
-->
<input type="hidden" name="num1" value="@value" />
<div>@value</div>
<input type="submit" value="自增"/>
</form>
</body>
</html>
-----------------------------------------------------------------
IncValue2.ashx
<%@ WebHandler Language="C#" Class="IncValue2" %>
using System;
using System.Web;
public class IncValue2 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string ispostback =context.Request["ispostback"];
string value = "0";
if (ispostback =="true") {
//这个时候是取不出值的
value=context.Request["num1"];//这个时候是取不到值
//注意,不是服务器读取网页,而是浏览器收集在表单中填写的字段,然后提交数据,
//给服务器来处理,由于没有把div当前的innertext发给服务器,所以服务器不能得知当前的值
//也不要幻想有办法能将div的innertext提交给服务器,因为只有设定了name的input,textarea
//select的value属性值才会被提交给服务器,只针对前面input,textarea,select这几个控件才能提交数据
int i = Convert.ToInt32(value);
i++;
value = Convert.ToString(i);
}
//把读出的html文件,替换值后,写回浏览器
string fullpath = context.Server.MapPath("IncValue2.htm");
string content = System.IO.File.ReadAllText(fullpath);
content = content.Replace("@value", value);
context.Response.Write(content);
//这里如果在页面中篡改了数据,就不会反应了,因为
//篡改的数据是div中的,而div的数据不会提交到后台
}
public bool IsReusable {
get {
return false;
}
}
}