什么是PostBack(譯)


什么是PostBack(譯) What is a postback?

下面的內容是針對ASP.NET新手的

PostBack什么時候被引發?

PostBack由客戶端瀏覽器引發。通常是用戶操作(點擊按鈕、修改下拉框等)頁面中的某個控件,然后該控件發起一個PostBack。最后該控件的狀態加上本頁面上所有的其它控件(也就是ViewState)一起回發到服務器。

PostBack時發生了什么?

通常情況下PostBack會觸發web服務器創建一個被引發了PostBack的頁面page類的實例。然后這個page對象就步入了它常規的頁面生命周期,與一般情況稍有差異(見下文)。如果你沒有在頁面的生命周期內將用戶重定向到另一個指定的頁面,PostBack的最終結果將是再次將相同的頁面呈現給用戶,然后開始等待下一個PostBack。

為什么需要PostBack?

web應用是運行在web服務器上的。為了處理用戶請求所導致的應用狀態的更改或是頁面的跳轉,你需要一些在web服務器上運行的代碼。實現該目標的唯一方法就是收集用戶正在使用的所有信息,然后將它們發回到服務器。

一些新手應該注意的事情

  • 控件的狀態在回發頁的環境中是可用的。通過這些信息你可以在后台操作頁面控件或是重定向到其它頁面。
  • WebForm里面的控件包含events(事件)、event handlers(事件處理器),頁面生命周期的初始化部分將在引發PostBack的控件的后台事件被調用前執行。即當用戶點擊按鈕時,會先執行Page Init和Load事件再執行按鈕單擊事件。
  • 頁面PostBack(回發)時,屬性“Page.IsPostBack”的值為“true”,其它情況(例如頁面初次載入)為“false”。
  • Ajax和MVC技術修改了PostBack的工作模式。

The following is aimed at beginners to ASP.Net...

When does it happen?

A postback originates from the client browser. Usually one of the controls on the page will be manipulated by the user (a button clicked or dropdown changed, etc), and this control will initiate a postback. The state of this control, plus all other controls on the page,(known as the View State) isPosted Back to the web server.

What happens?

Most commonly the postback causes the web server to create an instance of the code behind class of the page that initiated the postback. This page object is then executed within the normal page lifecycle with a slight difference (see below). If you do not redirect the user specifically to another page somewhere during the page lifecycle, the final result of the postback will be the same page displayed to the user again, and then another postback could happen, and so on.

Why does it happen?

The web application is running on the web server. In order to process the user’s response, cause the application state to change, or move to a different page, you need to get some code to execute on the web server. The only way to achieve this is to collect up all the information that the user is currently working on and send it all back to the server.

Some things for a beginner to note are...

  • The state of the controls on the posting back page are available within the context. This will allow you to manipulate the page controls or redirect to another page based on the information there.
  • Controls on a web form have events, and therefore event handlers, just like any other controls. The initialisation part of the page lifecycle will execute before the event handler of the control that caused the post back. Therefore the code in the page’s Init and Load event handler will execute before the code in the event handler for the button that the user clicked.
  • The value of the “Page.IsPostBack” property will be set to “true” when the page is executing after a postback, and “false” otherwise.
  • Technologies like Ajax and MVC have changed the way postbacks work.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM