以前写的课程都没有附上源码,很抱歉!
ASP.Net学习笔记007ASP.Net Input版自增.zip
http://credream.7958.com/down_20155694.html
1.常见错误:把html设置成了启动页
2.非表单元素无法把表单的值,传递给服务器端,即使是表单元素也只能传递value值,对于
其他值比如颜色,大小等,都需要使用隐藏字段,才能传递,这就是asp.net中的viewState
的实现原理
-----------------------------------
上节课代码:
IncValue.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="IncValue.ashx">
<input type="hidden" name="ispostback" value="true" />
<!--
<input type="submit" value="自增" />
type="button"的时候是不能提交的,如果想提交需要
-->
<input type="text" name="number" value="@value" />
<input type="button" value="自增" onclick ="document.getElementById('From1').submit()" />
</form>
</body>
</html>
-----------------------------------
IncValue.ashx还是上节课的,没有变
<%@ WebHandler Language="C#" Class="IncValue" %>
using System;
using System.Web;
public class IncValue : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string ispostback = context.Request["ispostback"];
string number = context.Request["number"];
//通过表单提交的都是string类型的
if (ispostback == "true")//说明点击了也没的自增按钮
{
int i = Convert.ToInt32(number);
i++;
number = i.ToString();
}
else {//第一次进入,值为0
number = "0";
}
//context.Response.Write(number);
string fullpath = context.Server.MapPath("IncValue.htm");
string content = System.IO.File.ReadAllText(fullpath);
content= content.Replace("@value", number);
context.Response.Write(content);
//context.Response.Write("你好");
}
public bool IsReusable {
get {
return false;
}
}
}
-------------------------------------------------------------------