IPostBackDataHandler 介面


定義 ASP.NET 伺服器控制項必須實作的方法,以自動載入回傳資料。Defines methods that ASP.NET server controls must implement to automatically load postback data.

public interface class IPostBackDataHandler
public interface IPostBackDataHandler
type IPostBackDataHandler = interface
Public Interface IPostBackDataHandler


下列程式碼範例將示範可實介面的自訂文字方塊伺服器控制項 IPostBackDataHandlerThe following code example demonstrates a custom text box server control that implements the IPostBackDataHandler interface. 由於 Text 回傳之後,屬性會變更,而伺服器控制項則會 TextChanged 在載入回傳資料之後引發事件。The Text property is changed as a result of the postback, and the server control raises a TextChanged event after postback data has been loaded.

using System;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;

namespace CustomWebFormsControls {

   [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
   public class MyTextBox: Control, IPostBackDataHandler {

      public String Text {
         get {
            return (String) ViewState["Text"];

         set {
            ViewState["Text"] = value;

      public event EventHandler TextChanged;

      public virtual bool LoadPostData(string postDataKey,
         NameValueCollection postCollection) {

         String presentValue = Text;
         String postedValue = postCollection[postDataKey];

         if (presentValue == null || !presentValue.Equals(postedValue)) {
            Text = postedValue;
            return true;

         return false;

      public virtual void RaisePostDataChangedEvent() {

      protected virtual void OnTextChanged(EventArgs e) {
         if (TextChanged != null)

      protected override void Render(HtmlTextWriter output) {
         output.Write("<INPUT type= text name = "+this.UniqueID
            + " value = " + this.Text + " >");

Imports System.Web
Imports System.Web.UI
Imports System.Collections
Imports System.Collections.Specialized

Namespace CustomWebFormsControls
    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> Public Class MyTextBox
        Inherits Control
        Implements IPostBackDataHandler
        Public Property Text() As String
                Return CType(Me.ViewState("Text"), String)
            End Get
                Me.ViewState("Text") = value
            End Set
        End Property
        Public Event TextChanged As EventHandler
        Public Overridable Shadows Function LoadPostData( _
        postDataKey As String, _
        postCollection As System.Collections.Specialized.NameValueCollection) _
        As Boolean Implements IPostBackDataHandler.LoadPostData
            Dim presentValue As String = Text
            Dim postedValue As String = postCollection(postDataKey)
            If presentValue Is Nothing Or Not presentValue.Equals(postedValue) Then
                Text = postedValue
                Return True
            End If
            Return False
        End Function
        Public Overridable Shadows Sub RaisePostDataChangedEvent() _
        Implements IPostBackDataHandler.RaisePostDataChangedEvent
        End Sub
        Protected Overridable Sub OnTextChanged(e As EventArgs)
            RaiseEvent TextChanged(Me, e)
        End Sub
        Protected Overrides Sub Render(output As HtmlTextWriter)
            output.Write("<INPUT type= text name = " & Me.UniqueID & _
                " value = " & Me.Text & " >")
        End Sub
    End Class
End Namespace


當您建立的 IPostBackDataHandler 伺服器控制項需要檢查用戶端回傳至伺服器的表單資料時,您必須執行此介面。You must implement the IPostBackDataHandler interface when creating a server control that needs to examine form data that is posted back to the server by the client. 此介面所定義的合約,可讓伺服器控制項判斷其狀態是否應在回傳後變更,以及引發適當的事件。The contract that this interface defines allows a server control to determine whether its state should be altered as a result of the postback, and to raise the appropriate events. 如需詳細資訊,請參閱 ASP.NET Web Forms 頁面中的伺服器事件處理For more information, see Server Event Handling in ASP.NET Web Forms Pages.


LoadPostData(String, NameValueCollection)

在類別實作時,處理 ASP.NET 伺服器控制項的回傳資料。When implemented by a class, processes postback data for an ASP.NET server control.


當類別實作時,發出信號給伺服器控制項,使其告知 ASP.NET 應用程式,控制項的狀態已變更。When implemented by a class, signals the server control to notify the ASP.NET application that the state of the control has changed.