IPostBackDataHandler Interfejs

Definicja

Definiuje metody, które ASP.NET kontrolek serwera muszą implementować, aby automatycznie ładować dane ogłaszania zwrotnego.

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

Przykłady

Poniższy przykład kodu przedstawia niestandardową kontrolkę serwera pola tekstowego, która implementuje IPostBackDataHandler interfejs. Właściwość Text jest zmieniana w wyniku ogłaszania zwrotnego, a kontrolka serwera zgłasza TextChanged zdarzenie po załadowaniu danych zwrotnych.

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() {
         OnTextChanged(EventArgs.Empty);
      }

      protected virtual void OnTextChanged(EventArgs e) {
         if (TextChanged != null)
            TextChanged(this,e);
      }

      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
            Get
                Return CType(Me.ViewState("Text"), String)
            End Get
            
            Set
                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
        
            OnTextChanged(EventArgs.Empty)
        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

Uwagi

Interfejs należy zaimplementować IPostBackDataHandler podczas tworzenia kontrolki serwera, która musi sprawdzać dane formularza, które są publikowane z powrotem na serwerze przez klienta. Kontrakt definiowany przez ten interfejs umożliwia kontroli serwera określenie, czy jego stan powinien zostać zmieniony w wyniku ogłaszania zwrotnego, oraz wywołanie odpowiednich zdarzeń. Aby uzyskać więcej informacji, zobacz Server Event Handling in ASP.NET Web Forms Pages (Obsługa zdarzeń serwera na stronach ASP.NET Web Forms).

Metody

LoadPostData(String, NameValueCollection)

Po zaimplementowaniu przez klasę dane ogłaszania zwrotnego dla kontroli serwera ASP.NET.

RaisePostDataChangedEvent()

Po zaimplementowaniu przez klasę sygnalizuje kontrolę serwera, aby powiadomić aplikację ASP.NET o zmianie stanu kontrolki.

Dotyczy

Zobacz też