IPostBackDataHandler Interfaz

Definición

Define los métodos que los controles de servidor ASP.NET deben implementar para cargar automáticamente datos devueltos.

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

Ejemplos

En el ejemplo de código siguiente se muestra un control de servidor de cuadro de texto personalizado que implementa la IPostBackDataHandler interfaz . La Text propiedad se cambia como resultado del postback y el control de servidor genera un TextChanged evento después de cargar los datos de postback.

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

Comentarios

Debe implementar la IPostBackDataHandler interfaz al crear un control de servidor que necesite examinar los datos del formulario que el cliente envía al servidor. El contrato que define esta interfaz permite a un control de servidor determinar si su estado se debe modificar como resultado del postback y generar los eventos adecuados. Para obtener más información, vea Control de eventos de servidor en páginas de ASP.NET Web Forms.

Métodos

LoadPostData(String, NameValueCollection)

Cuando se implementa mediante una clase, se procesan los datos devueltos para un control de servidor ASP.NET.

RaisePostDataChangedEvent()

Cuando se implementa mediante una clase, indica al control de servidor que notifique a la aplicación ASP.NET que el estado del control ha cambiado.

Se aplica a

Consulte también