ASP.NET中IsPostBack的理解

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   for   sp   檔案   

這個屬性相信大家一定經常用吧 ,判斷是否為回傳 (切記這是判斷是否回傳 ,而聽到很多人說這是判斷是否第一次載入頁面 ,還有的說是否為重新整理 )很多人說做項目時pageload事件裡都要加上,例如下面的代碼:

protected void Page_Load(Object sender,EventArgs e)

{

     if(!IsPostBack)

      {

           //執行頁面首次請求時需要進行的操作;

        }

}

 

 

但是確不太理解根本原,說加上這個肯定沒錯 ,然而不知根本終會出錯。 所以 一定要理解原理~~

為了把這個說清楚 ,這裡不用asp.net頁面 用html+handler 一般處理常式來講清這個屬性。如下:

<html xmlns="http://www.w3.org/1999/xhtml"><head> <title>關於IsPostBack的理解</title></head><body> <form action="Hello.ashx" method="post">    <input type ="hiddle" name="isPostBack" class="haddile" name="isPostBack" value="true"/> <label>姓名:</label> <input type="text" id="txtUserName" name="txtUserName" value ="{userName}"/> <label>年齡:</label> <input type="text" id="txtUserAge" name="txtUserAge" value ="{userAge}" /> <input type="submint" value="提交" /> <label>{ErrorMsg}</label></form></body> 相關HttpHandler代碼如下:publci void ProcessRequset(HttpContext context){ context.Response.ContextType = "text/html"; string userName = context.Request["txtUserName"]; string userAge = context.Request["txtUserAge"]; string msg = string.Empty; string isPostBack = context.Request["isPostBack"]; if(isPostBack == "true")//如果提交了該參數則說明是回傳提交表單進行請求頁面 { context.Response.Write("表單提交回傳請求頁面"); msg = userName +"你好!"; }else{ context.Response.Write("首次請求頁面"); userName = string.Empty; mag = string.Empty; userAge = string.Empty;}string fulPath = context.Server.MapPath("Hello.html");//得到檔案全路徑string content = System.IO.File.ReadAllText(fullPath);//讀取檔案內容content =content.Replace("{txtUserName}",userName);content = content.Replace("{txtUserAge}",userAge);content = content.Replace("{ErrorMsg}".msg,);context.Response.Write(content);}public bool IsReusable{ get{ return false;}}

 閱讀代碼相信大家一看就知道怎麼回事兒,這裡簡要說下

運行HttpHandler因為第一次運行context.Request["isPostBack"]擷取傳進來的參數肯定是為null,所以會直接進入頁面讀取html,把首次請求處理的資料輸出到html頁面上。

(預留位置{txtUserName})為了實現儲存頁面狀態哦。

這時在html頁面輸入姓名和年齡點擊提交時(包括以後提交)

 <input type="hidden" name="ispostback" class="night" value="true" />

isPostBack的Value一直是True,所以context.Request["isPostBack"]擷取參數的值為“true”;這就造成了很多人說isPostBack是判斷是否為第一次請求頁面,其實當你進入頁面後進行重新整理操作時,依然會發現isPostBack不為True,原因是你並沒有進行提交是想一次回傳操作。

舉個生活的執行個體吧

假如你去餐館吃飯,那麼餐館就是相當於伺服器,你就是用戶端,那麼餐館服務員提交給你的菜單(相當於是網頁首次開啟,進行首次請求),然後你從菜單上點菜後將菜單返回給餐館服務員(這個過程相當於是進行isPsotBack回傳請求)。餐館根據你的菜單做出你需要的才,上到你餐桌上(進行isPostBack後伺服器返回給你的頁面)。

 其中的原理是就是這樣,如各位有更好的理解說明,不妨貼出來分享下!!!

ASP.NET中IsPostBack的理解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.