Postback Data Processing Sample

This sample shows a custom text box that has a text property that changes as a result of postback. The control raises a TextChanged event after it loads postback data. To build the sample, see the instructions in Server Control Samples.

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

namespace CustomControls{
      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 values) {
                  String presentValue = Text;
                  String postedValue = values[postDataKey];
                  if (!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.AddAttribute(HtmlTextWriterAttribute.Type, "text");
output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
            }
      }    
}
[Visual Basic]
Option Explicit
Option Strict

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

Namespace CustomControls
   Public Class MyTextBox
      Inherits Control
      Implements IPostBackDataHandler
      
      Public Property Text() As String
         Get
            Return CType(ViewState("Text"), String)
         End Get
         Set
            ViewState("Text") = value
         End Set
      End Property
      
      Public Event TextChanged As EventHandler
      
      Public Overridable Function LoadPostData(postDataKey As String, values As NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
         Dim presentValue As String = Text
         Dim postedValue As String = values(postDataKey)
         If Not presentValue.Equals(postedValue) Then
            Text = postedValue
            Return True
         End If
         Return False
      End Function
      
      Public Overridable 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.AddAttribute(HtmlTextWriterAttribute.Type, "text")
output.AddAttribute(HtmlTextWriterAttribute.Value, me.Text)
output.AddAttribute(HtmlTextWriterAttribute.Name, me.UniqueID)
output.RenderBeginTag(HtmlTextWriterTag.Input)
output.RenderEndTag()
      End Sub
   End Class
End Namespace

Using the Control on a Page

The following ASP.NET page uses the custom control MyTextBox, defined in the preceding sample.

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="C#" runat=server>
  private StringBuilder message = new StringBuilder("");

      private void Text_Changed(Object sender,EventArgs e){
            message.Append("The text in" + sender.ToString()+ " was changed.");
            message.Append("<br>You entered " + Server.HtmlEncode(Box.Text) +".");
      }
            
      protected override void Render(HtmlTextWriter output) {
            base.Render(output);
            output.Write(message.ToString());
      }     
</script>
            
<body>
                  
<form method="POST" action="MyTextBox.aspx" runat=server>
The text box below is an instance of a custom text box.<br>
It raises a text changed event when the text in the text box changes.<br><br>      
Enter your name:   <Custom:MyTextBox Text=" " OnTextChanged = "Text_Changed" id = "Box"  runat=server/>                                
<br><br>
<asp:Button Text = "Submit" runat = server/>                                                                              
</form>                                               
</body>                                          
</html> 
[Visual Basic]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="VB" runat=server>
   Private message As New StringBuilder("")

   Private Sub Text_Changed(sender As Object, e As EventArgs)
      message.Append(("The text in" & sender.ToString() & " was changed."))
      message.Append(("<br>You entered " & Server.HtmlEncode(Box.Text) & "."))
   End Sub

   Protected Overrides Sub Render(output As HtmlTextWriter)
      MyBase.Render(output)
      output.Write(message.ToString())
   End Sub
</script>
            
<body>
                  
<form method="POST" action="MyTextBox.aspx" runat=server>
The text box below is an instance of a custom text box.<br>
It raises a text changed event when the text in the text box changes.<br><br>      
Enter your name:   <Custom:MyTextBox Text=" " OnTextChanged = "Text_Changed" id = "Box"  runat=server/>                                
<br><br>
<asp:Button Text = "Submit" runat = server/>                                                                              
</form>                                               
</body>                                          
</html>

See Also

Processing Postback Data