Can't get TextChanged event to work on a custom control RRS feed

  • Question

  • User1067945151 posted


    I'm  trying to teach myself asp.net with the help of a book that I bought years ago but never got around to reading. As things have moved on a bit I'm finding that certain things don't quite work but up until now I've managed to sort them out with the help of the internet and Visual Studio auto correcting things for me.

    However, I've now got to a section on creating an "event wary custom control" and it doesn't seem to be working properly. The custom control is very simple - a text box that maintains viewstate and has a TextChanged event. The viewstate is maintained without any problems but the TextChanged event doesn't seem to be firing. Can anyone help?


    The Custom control is a vb file that I'm compiling with vbc.exe. The code is as follows:

    Imports System
    Imports System.Web
    Imports System.Web.UI
    Imports System.Collections.Specialized
    Namespace MyCustomControls
        Public Class CustomControl3 : Inherits Control : Implements IPostBackDataHandler
            Public Event TextChanged(ByVal obj As Object, ByVal e As EventArgs)
            Protected Sub OnTextChanged(ByVal e As EventArgs)
                RaiseEvent TextChanged(Me, EventArgs.Empty)
            End Sub
            Public Function LoadPostData(ByVal PostDataKey As String, ByVal Values As NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
                Dim strOldValue As String = Me.Message
                Dim strNewValue As String = Values(PostDataKey)
                If Not strOldValue = strNewValue Then
                    Me.Message = strNewValue
                End If
                Return False
            End Function
            Public Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
            End Sub
            Public Property Message() As String
                    Message = ViewState("Message").ToString
                End Get
                Set(ByVal value As String)
                    ViewState("Message") = value
                End Set
            End Property
            Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
                Output.Write("<input name=" & Me.UniqueID & " type=text value=""" & Me.Message & """>")
            End Sub
        End Class
    End Namespace


    This is then registered and used in the following aspx page:

    <%@ Page Language="VB" %>
    <%@ Register TagPrefix="ACME" Namespace="MyCustomControls" Assembly="CustomControls" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
        Sub Submit(ByVal obj As Object, ByVal e As EventArgs)
            'do nothing
        End Sub
        Sub ChangeIt(ByVal obj As Object, ByVal e As EventArgs)
            lblMessage.Text = "Event Handled!"
        End Sub
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <form id="form1" runat="server">
            <p>The custom control produces the following output:</p>
            <ACME:CustomControl3 ID="MyControl" runat="server" Message="Hello World!" OnTextChanged="ChangeIt" />
            <asp:Button runat="server" Text="Submit" OnClick="Submit" />
            <asp:Label runat="server" ID="lblMessage" />


    Wednesday, December 16, 2009 7:04 AM


  • User1067945151 posted

    Worked it out - I'd failed to include a return true in the loadPostData function. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 16, 2009 8:17 AM