.net通过一般处理程序模拟用户控件数据保持、Ispostback 【第二版将html与ashx文件分开】...
生活随笔
收集整理的這篇文章主要介紹了
.net通过一般处理程序模拟用户控件数据保持、Ispostback 【第二版将html与ashx文件分开】...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接上代碼:
HTML代碼,其中表單中input標簽中的value值首先用一個占位符占位,然后在ashx中將其替換
<html xmlns="http://www.w3.org/1999/xhtml"> <head><title></title> </head> <body> <form action = "02jsqHandler.ashx" method = "post"><input type = "text" name = "txtNum1" value = "@txtNum1" />+<input type = "text" name = "txtNum2" value = "@txtNum2" />=<input type = "text" name = "txtResult" value = "@txtResult" /><input type = "hidden" name = "IsPostBack" value = "false" /><input type = "submit" value = "計算" /> </form> </body> </html>ashx代碼:
首先用io中的File.ReadAllText()方法將html文件讀出來
public class _02jsqHandler : IHttpHandler {public void ProcessRequest (HttpContext context) {context.Response.ContentType = "text/html";//獲得html文件的內容string path = context.Server.MapPath("03jsq2.htm");string strHtml = File.ReadAllText(path);//聲明三個整形變量int n1 = 0, n2 = 0, result = 0;bool ispostback = Convert.ToBoolean(context.Request.Form["IsPostBack"]);if (!ispostback) //判斷是否是第一次加載相當于aspx.cs中的IsPostBack {//分別獲得數1和數2string sn1 = context.Request.Form["txtNum1"];string sn2 = context.Request.Form["txtNum2"];if (int.TryParse(sn1, out n1) && int.TryParse(sn2, out n2)){result = n1 + n2;}//context.Response.Write(result.ToString()); }//將html中的@參數(占位符替換為對應的數據)strHtml = strHtml.Replace("@txtNum1", n1.ToString()).Replace("@txtNum2", n2.ToString()).Replace("@txtResult", result.ToString());//將html寫回頁面 context.Response.Write(strHtml);}public bool IsReusable {get {return false;}}?
轉載于:https://www.cnblogs.com/wlitsoft/archive/2012/04/20/2460603.html
總結
以上是生活随笔為你收集整理的.net通过一般处理程序模拟用户控件数据保持、Ispostback 【第二版将html与ashx文件分开】...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ext.form.TextArea文本区
- 下一篇: linux管理磁盘和文件系统