IPostBackDataHandler 接口

定义

定义 ASP.NET 服务器控件为自动加载回发数据而必须实现的方法。

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

示例

下面的代码示例演示了实现 接口的 IPostBackDataHandler 自定义文本框服务器控件。 属性 Text 因回发而更改,服务器控件在加载回发数据后引发 TextChanged 事件。

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

注解

在创建需要检查客户端发回到服务器的表单数据的服务器控件时,必须实现 IPostBackDataHandler 接口。 此接口定义的协定允许服务器控件确定是否应因回发而更改其状态,并引发相应的事件。 有关详细信息,请参阅 ASP.NET Web 窗体页中的服务器事件处理

方法

LoadPostData(String, NameValueCollection)

当由某个类实现时,它为 ASP.NET 服务器控件处理回发数据。

RaisePostDataChangedEvent()

当由某个类实现时,它用信号要求服务器控件通知 ASP.NET 应用程序该控件的状态已更改。

适用于

另请参阅