Page.RegisterRequiresPostBack(Control) Page.RegisterRequiresPostBack(Control) Page.RegisterRequiresPostBack(Control) Method


將控制項註冊為,當頁面回傳給伺服器時,需要回傳處理的控制項。Registers a control as one that requires postback handling when the page is posted back to the server.

 void RegisterRequiresPostBack(System::Web::UI::Control ^ control);
public void RegisterRequiresPostBack (System.Web.UI.Control control);
member this.RegisterRequiresPostBack : System.Web.UI.Control -> unit


Control Control Control

要登錄的控制項。The control to be registered.


要註冊的控制項不會實作 IPostBackDataHandler 介面。The control to register does not implement the IPostBackDataHandler interface.


下列程式碼範例會使用RegisterRequiresPostBack方法,要求在與文字方塊相關聯myTextBox的任何程式碼可以執行之前,將文字方塊控制項回傳。The following code example uses the RegisterRequiresPostBack method to require a text box control, myTextBox, to be posted back before any code associated with the text box can execute. TextBox控制項會執行IPostBackDataHandler介面。TextBox controls implement the IPostBackDataHandler interface.


這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

protected void Text_Change(object sender, EventArgs e)
  myLabel.Text = "<b>Welcome " + myTextBox.Text + " to ASP.NET</b>";

protected void Page_PreRender(object sender, EventArgs e)
Sub Text_Change(ByVal sender As Object, ByVal e As EventArgs)
  myLabel.Text = "<b>Welcome " + myTextBox.Text + " to ASP.NET</b>"
End Sub 'Text_Change

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub


要註冊的控制項必須執行IPostBackDataHandler介面, HttpException否則會引發。The control to be registered must implement the IPostBackDataHandler interface or an HttpException is raised. 由控制項執行時,此IPostBackDataHandler介面可讓您處理回傳資料,並引發任何 post 資料已變更的事件。When implemented by a control, the IPostBackDataHandler interface enables handling of post back data and raising of any post back data changed events. 如需伺服器控制項事件模型的詳細資訊,請參閱ASP.NET Web Form 伺服器控制項事件模型For more information on the server control event model, see ASP.NET Web Forms Server Control Event Model.

在頁面生命週期的Page_PreRender事件發生時,向頁面註冊控制項。Register controls with the page at or before the Page_PreRender event of the page life cycle.