版本一:
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="go.ashx" method="post"> <input type="text" name="txtnum1" value="0" /> <input type="text" name="txtnum2" value="0" /> <input type="text" name="txtnum3" value="0" /> <input type="submit" value="提交" /> </form></body></html>
一般處理常式
<%@ WebHandler Language="C#" Class="go" %>using System;using System.Web;public class go : IHttpHandler { public void ProcessRequest (HttpContext context) { string strnum1 = context.Request.Form["txtnum1"]; string strnum2 = context.Request.Form["txtnum2"]; int x = 0,y=0,z=0; if (!string.IsNullOrEmpty(strnum1) && !string.IsNullOrEmpty(strnum2)) { if (int.TryParse(strnum1, out x) && int.TryParse(strnum2, out y)) { z = x + y; } } System.Text.StringBuilder sbHtml = new System.Text.StringBuilder(); sbHtml.Append("<html ><head><title></title></head>"); sbHtml.Append("<body><form action='' method='post'>"); sbHtml.Append("<input type='text name='txtnum1' value='"+x.ToString()+"'/>+<input type='text' name='txtnum2' value='"+y.ToString()+"'/>"); sbHtml.Append(""+"<input type='text' name='txtnum3' value='"+z.ToString()+"' />+<input type='submit' value='提交' />"); sbHtml.Append("</form></body></html>"); //context.Response.ContentType = "text/plain"; context.Response.Write(sbHtml.ToString()); } public bool IsReusable { get { return false; } }}
版本二:
模版預留位置:
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="go.ashx" method="post"> <input type="text" name="txtnum1" value='{num1}' /> <input type="text" name="txtnum2" value='{num2}' /> <input type="text" name="txtnum3" value='{num3}' /> <input type="submit" value="提交" /> </form></body></html>
一般處理常式
<%@ WebHandler Language="C#" Class="go" %>using System;using System.Web;public class go : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/html"; string path = context.Server.MapPath("computer.htm"); string sbHtml = System.IO.File.ReadAllText(path); string strnum1 = context.Request.Form["txtnum1"]; string strnum2 = context.Request.Form["txtnum2"]; int x = 0,y=0,z=0; if (!string.IsNullOrEmpty(strnum1) && !string.IsNullOrEmpty(strnum2)) { if (int.TryParse(strnum1, out x) && int.TryParse(strnum2, out y)) { z = x + y; } } sbHtml= sbHtml.Replace("{num1}",x.ToString()).Replace("{num2}", y.ToString()).Replace("{num3}", z.ToString()); //context.Response.ContentType = "text/plain"; context.Response.Write(sbHtml); } public bool IsReusable { get { return false; } }}