Adding an asynchronous callback to validate data (VB) : Validation « Custom Controls « ASP.NET Tutorial






Imports System.ComponentModel
Imports System.Web.UI

<DefaultProperty("Text"), _
 ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")> _
Public Class WebCustomControl1
    Inherits System.Web.UI.WebControls.WebControl
    Implements System.Web.UI.ICallbackEventHandler

    Dim _text As String

    <Bindable(True), Category("Appearance"), Themeable(False), DefaultValue("")> _
    Property [Text]() As String
        Get
            Return _text
        End Get

        Set(ByVal Value As String)
            _text = Value
        End Set
    End Property


    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
        output.RenderBeginTag(HtmlTextWriterTag.Div)

        output.AddAttribute(HtmlTextWriterAttribute.Type, "text")
        output.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
        output.AddAttribute(HtmlTextWriterAttribute.Name, Me.ClientID)
        output.AddAttribute(HtmlTextWriterAttribute.Value, Me.Text)

        output.AddAttribute("OnBlur", "ClientCallback();")
        Me.AddAttributesToRender(output)

        output.RenderBeginTag(HtmlTextWriterTag.Input)
        output.RenderEndTag()

        output.RenderEndTag()
    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        Page.ClientScript.RegisterStartupScript(GetType(Page), _
           "ControlFocus", "document.getElementById('" & Me.ClientID & "').focus();", _
           True)

        Page.ClientScript.RegisterStartupScript( _
            GetType(Page), "ClientCallback", _
            "function ClientCallback() {" & _
                "args=document.getElementById('" & Me.ClientID & "').value;" & _
                Page.ClientScript.GetCallbackEventReference(Me, "args", _
                    "CallbackHandler", Nothing, "ErrorHandler", True) + "}", _
            True)
    End Sub


    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        Dim result As Int32
        If (Not Int32.TryParse(eventArgument, result)) Then
            Throw New Exception("The method or operation is not implemented.")
        End If
    End Sub

    Public Function GetCallbackResult() As String Implements ICallbackEventHandler.GetCallbackResult
        Return "Valid Data"
    End Function

End Class








14.18.Validation
14.18.1.Adding an asynchronous callback to validate data (C#)
14.18.2.Adding an asynchronous callback to validate data (VB)