杨中科讲的最简单的调用ashx文件--一般处理文件
以下是input(textbox版本)的自增,不需要隐藏域--因为是html文件,也就是静态页。
div的例子是特例,div是属于lable版本的。但是在html文件(静态页)不用隐藏域,div不可以自增。
但是如果是aspx页面(动态页面)。我们就可以再cs文件中让div自增--系统自动保存在viewstate中。
上面的代码调用下面的代码:
还有就是特别注意一下:
html里面的id是针对dom
html里面的name是针对服务器(服务器只认name,不认识id)
一个稍微复杂点的例子
在一般处理文件中:
点击一下自增加1的例子
div版本的自增,把占位符藏在hidden里面,再传给div
关于禁止ViewState
第一种。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" EnableViewState="false" %>
第二种。在个别控件中禁止,就让个别控件的EnableViewState="false"
以下是input(textbox版本)的自增,不需要隐藏域--因为是html文件,也就是静态页。
div的例子是特例,div是属于lable版本的。但是在html文件(静态页)不用隐藏域,div不可以自增。
但是如果是aspx页面(动态页面)。我们就可以再cs文件中让div自增--系统自动保存在viewstate中。
<form id="form1" action="Handler2.ashx">
<div>
姓名:<input type="text" name="xu" /><input type="submit" value="提交"/>
</div>
</form>
上面的代码调用下面的代码:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string name = context.Request["xu"];
context.Response.Write(name);
}
还有就是特别注意一下:
html里面的id是针对dom
html里面的name是针对服务器(服务器只认name,不认识id)
一个稍微复杂点的例子
在一般处理文件中:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string fullpath = context.Server.MapPath("需要重写的页面的完整路径");
string content = System.IO.File.ReadAllText(fullpath);
context.Response.Write(content);
string name = context.Request["xu"];
if (string.IsNullOrEmpty(name))
{
context.Response.Write("直接进入");
}
else
{
context.Response.Write("登陆后进入");
}
}
<form id="form1" action="Handler2.ashx"> //action="Handler2.ashx"就是调用Handler2.ashx
<div>
nihao:<input type="text" name="xu" /><input type="submit" value="提交"/>
</div>
</form>
点击一下自增加1的例子
<form action="Handler2.ashx">
<input type="hidden" name="ispostback" value="true" />
<input type="text" name="number" value="@value" /><input type="submit" value="点击一下就加一" />
</form>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string IsPostBack = context.Request["ispostback"];
string mynumber = context.Request["number"];
if (IsPostBack == "true")//点了submit属性的按钮的
{
int i = Convert.ToInt32(mynumber);
i++;
mynumber = i.ToString();
}
else//第一次进入
{
mynumber = "0";
}
string fullpath = context.Server.MapPath("HTMLPage1.htm");
string content = System.IO.File.ReadAllText(fullpath);
content = content.Replace("@value", mynumber);
context.Response.Write(content);
}
div版本的自增,把占位符藏在hidden里面,再传给div
<form action="Handler_ADD.ashx">
<input type="hidden" name="ispostback" value="true"/>
<input type="hidden" name="num" value="@m" />
<div>@m</div>
<input type="submit" value="点一下就加一" />
</form>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string IsPostBack = context.Request["ispostback"];
string value = "0";
if (IsPostBack == "true")
{
value = context.Request["num"];
int i = Convert.ToInt32(value);
i++;
value = i.ToString();
}
string fullpath = context.Server.MapPath("ADD.htm");
string content = System.IO.File.ReadAllText(fullpath);
content = content.Replace("@m", value);
context.Response.Write(content);
}
关于禁止ViewState
第一种。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" EnableViewState="false" %>
第二种。在个别控件中禁止,就让个别控件的EnableViewState="false"