一般处理程序学习初步——最简单的一般处理程序
代码示例
html部分代码
<!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 ="hello.ashx" method =post> <table border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td> <input type="text" name = "txtNum1" value= "@txtNum1" /> </td> <td> + </td> <td> <input type="text" name = "txtNum2" value= "@txtNum2"/> </td> <td> <input type="submit" value = "="> </td> <td> <input type="text" name = "txtSum" value= "@txtSum"/> </td> </tr> </table> <input type="hidden" name ="isPostBack" value="1"/> </form> </body> </html>
aspx代码
<%@ WebHandler Language="C#" Class="hello" %>
using System;
using System.Web;
public class hello : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
//context.Response.Write("Hello World");
string strPath = context.Request.MapPath("hello.htm");
string strHtml = System.IO.File.ReadAllText(strPath);
string strNum1 = context.Request.Form["txtNum1"];
string strNum2 = context.Request.Form["txtNum2"];
int num1 = 0;
int num2 = 0;
int.TryParse(strNum1, out num1);
int.TryParse(strNum2, out num2);
int sum = num1 + num2;
string newStrHtml = "";
if (string.IsNullOrEmpty(context.Request.Form["isPostBack"]))
{
newStrHtml = strHtml.Replace("@txtNum1", "").Replace("@txtNum2", "").Replace("@txtSum", "");
}
else
{
newStrHtml = strHtml.Replace("@txtNum1", strNum1).Replace("@txtNum2", strNum2).Replace("@txtSum", sum.ToString());
}
context.Response.Write(newStrHtml);
}
public bool IsReusable {
get {
return false;
}
}
}

浙公网安备 33010602011771号