基于ashx(一般处理程序)的Asp.Net编程,对服务端控件实现原理的理解.
一、任务:
任务1:用一般处理程序实现一个用户登录页面,输入正确提示登录成功,输入错误提示登录失败。访问页面是(Login.ashx)
任务2:用一般处理程序实现InputBox版自增程序。
任务3:用一般处理程序实现DIV版自增程序。
二、任务实现:
(1)任务1:登录表单和提交表单的处理都在同一页面Login.ashx,所以需要对用户对Login.ashx的访问进行判断,如果是第一次请求(直接访问)就将登录表单输出给用户,如果是因为提交表单对Login.ashx的请求才对用户提交数据进行处理。(这个也是后面Pager对象的IsPostback的作用,但是这里我们需要自己处理,实现也很简单。)为了方便表单的输出,我们把表单单独设计在一个静态Login.html文件中。
Login.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>
<style>
span {
display:block;
}
</style>
</head>
<body>
<form action="Login.ashx" method="post">
<span>用户:<input type="text" name="username" /></span>
<span>密码:<input type="password" name="pass"/></span>
<span><input type="submit" value="提交" name="submit"/></span>
</form>
</body>
</html>
Login.ashx代码:
<%@ WebHandler Language="C#" Class="Login" %>
using System;
using System.Web;
public class Login : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string username = context.Request["username"];
string pass = context.Request["pass"];
string isposback=context.Request["submit"];
if (isposback == "提交")
{
if (username == "admin" && pass == "88888")
{
context.Response.Write("登录成功");
}
else
{
context.Response.Write("登录失败");
}
}
else
{
string content = System.IO.File.ReadAllText(context.Server.MapPath("Login.html"));
context.Response.Write(content);
}
}
public bool IsReusable {
get {
return false;
}
}
}
(2)任务2:这里同样用IncNumberInput.html来保存html表单信息,要注意的时每次点击自增按钮后需要把自增后的值回发填充到Input中。(服务端控件就做了这个工作),这里我们用Replace函数来实现回发时值的替换。
IncNumberInput.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="incNumberInput.ashx.ashx" method="post">
<input type="text" name="txt" value="@value"/><!--@value是用于替换的占位符-->
<input type="submit" value="自增" name="submit"/>
</form>
</body>
</html>
incNumberInput.ashx代码如下:
<%@ WebHandler Language="C#" Class="incNumberInput" %>
using System;
using System.Web;
public class incNumberInput : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string tj=context.Request["submit"];//获取提交按钮的值
string txt=context.Request["txt"];//获取文本框的值
string Snum = null;
string content = System.IO.File.ReadAllText(context.Server.MapPath("Inc01.html"));
if (string.IsNullOrEmpty(tj))//如果tj为空,说明用户是直接访问
{
Snum = "1";
}
else
{
Snum = (Convert.ToInt32(txt) + 1).ToString();
}
content = content.Replace("@value", Snum);//将文本框的内容替换成自加后的值。
context.Response.Write(content);
}
public bool IsReusable {
get {
return false;
}
}
}
(3)任务3:任务3和任务2的区别在于,用Div实现自增效果的话必须用一个隐藏域保存自增的值。因为Div是一个非表单元素,非表单元素的值是不能直接提交给服务器处理的,所以需要用一个同步的表单元素,但这个表单元素又不能让用户可见,所以是隐藏域.(这就是隐藏域的巨大作用,也可以说ViewState的作用就是这样,用于保存一些客户端非表单元素的状态信息。)
incNumberDiv.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="incNumberDiv.ashx" method="post">
<div>@value</div>
<input type="hidden" name="num" value="@value">
<input type="submit" name="submit" value="自增"/>
</form>
</body>
</html>
incNumberDiv.ashx代码如下:
<%@ WebHandler Language="C#" Class="incNumberDiv" %>
using System;
using System.Web;
public class incNumberDiv : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string ispostback=context.Request["submit"];
string num=context.Request["num"];
string value = "0";
string content = "";
if (!string.IsNullOrEmpty(ispostback))
{
value = (Convert.ToInt32(num) + 1).ToString();
}
content = System.IO.File.ReadAllText(context.Server.MapPath("incNumberDiv.html"));
content = content.Replace("@value",value);
context.Response.Write(content);
}
public bool IsReusable {
get {
return false;
}
}
}