值得收藏的asp.net基础学习笔记(3)

public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/html"; string strNum1 = context.Request.Form["txtNum1" ]; string strNum2 = context.Request.Form["txtNum2" ]; //判断是否格式正确 string result = "0" ; int num1 = 0, num2 = 0; if (!string .IsNullOrEmpty(strNum1) && ! string.IsNullOrEmpty(strNum2)) { if (int .TryParse(strNum1, out num1) && int.TryParse(strNum2, out num2)) { result = (num1+num2).ToString(); } else { result = "输入格式错误" ; } } System.Text. StringBuilder sbHTML = new System.Text.StringBuilder(); sbHTML.Append( "<!DOCTYPE><html><head><title>计算器</title></head><body><form action='06Calculate.ashx' method='post'>"); sbHTML.Append( "<input type='text' value='" + num1.ToString() + "' /> + <input type='text'name='txtNum2' value='" + num2.ToString() + "'/> = <input type='text' readonly='readonly' value='" + result.ToString() + "' <br/>"); sbHTML.Append( "<input type='submit' value='计算'/><br />" ); sbHTML.Append( "</form></body></html>" ); context.Response.Write(sbHTML.ToString()); }

------------------模拟WebForm回传检查机制------------------ 

public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/html"; string strNum1 = context.Request.Form["txtNum1" ]; string strNum2 = context.Request.Form["txtNum2" ]; //判断是否格式正确 string result = "0" ; int num1 = 0, num2 = 0; //如果包含隐藏域的话, 才执行相加操作 if (!string .IsNullOrEmpty(context.Request.Form[ "hidIsPostBack"])) { if (!string .IsNullOrEmpty(strNum1) && ! string.IsNullOrEmpty(strNum2)) { if (int .TryParse(strNum1, out num1) && int.TryParse(strNum2, out num2)) { result = (num1 + num2).ToString(); } else { result = "输入格式错误" ; } } } System.Text. StringBuilder sbHTML = new System.Text.StringBuilder(); sbHTML.Append( "<!DOCTYPE><html><head><title>计算器</title></head><body><form action='06Calculate.ashx' method='post'>"); sbHTML.Append( "<input type='text' value='" + num1.ToString() + "' /> + <input type='text'name='txtNum2' value='" + num2.ToString() + "'/> = <input type='text' readonly='readonly' value='" + result.ToString() + "' <br/>"); sbHTML.Append( "<input type='submit' value='计算'/><br />" ); sbHTML.Append( "<input type='hidden' value='1' /></form></body></html>" ); context.Response.Write(sbHTML.ToString()); }

----------------------------基于面向对象的计算器----------------------------

//---------------------------------类定义-------------------------------------------- /// <summary> ///一个计算器类 /// </summary> public class Class1 { //第一个操作数 public int num1 { get; set; } //第二个操作数 public int num2 { get; set; } //操作符 public string calculateChar{ get; set; } //结果 public string result { get; set; } public Class1() { } /// <summary> /// 计算结果 /// </summary> /// <param> 第一个操作数 </param> /// <param> 第二个操作数 </param> /// <param> 操作符</param> public void GetResult(int a, int b, string oper) { this.num1 = a; this.num2 = b; this.calculateChar = oper; switch (this .calculateChar) { case "+" : result = (num1 + num2).ToString(); break; case "-" : result = (num1 - num2).ToString(); break; case "*" : result = (num1 * num2).ToString(); break; case "https://www.jb51.net/" : result = (num1 / num2).ToString(); break; } } } //------------------------------------------------------页面类---------------------------------------------------------- public class _07CalculateFour : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/html"; //实例化一个计算器对象 Class1 calcu = new Class1(); string strNum1 = context.Request.Form["txtNum1" ]; string strNum2 = context.Request.Form["txtNum2" ]; string strOper = context.Request.Form["optionOper" ]; int num1 = 0; int num2 = 0; if (!string .IsNullOrEmpty(context.Request.Form[ "hidIsPostBack"])) { //模拟回访 if (!string .IsNullOrEmpty(strNum1) && ! string.IsNullOrEmpty(strNum2)) { //判断为空 if (int .TryParse(strNum1, out num1) && int.TryParse(strNum2, out num2)) { //判断格式 calcu.GetResult(num1, num2, strOper); } else { calcu.result = "参数格式不正确" ; } } } System.Text. StringBuilder sbHTML = new System.Text.StringBuilder(); sbHTML.Append( "<!DOCTYPE ><html><head></head><body><form action='07CalculateFour.ashx' method='post'>"); sbHTML.Append( "<input type='text' value='" +calcu.num1.ToString()+"'/>"); sbHTML.Append( "<select><option value='"+calcu.calculateChar+ "'>"+calcu.calculateChar+"</option><option value='+'>+</option><option value='-'>-</option><option value='*'>*</option><option value='https://www.jb51.net/'>/</option></select>" ); sbHTML.Append( "<input type='text' value='" +calcu.num2.ToString()+"'/> = "); sbHTML.Append( "<input type='text' readonly='readonly' value='" +calcu.result+"'/>"); sbHTML.Append( "<input type='submit' value='计算'/>" ); sbHTML.Append( "<input type='hidden' value='1'/></form></body></html>" ); context.Response.Write(sbHTML.ToString()); }

10.注意,关于提交表单的内容 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjydzs.html