Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<DefaultProperty("Text"), _
ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")> _
Public Class ServerControl27
    Inherits WebControl
    Implements ICallbackEventHandler

    <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> _
    Property Text() As String
        Get
            Dim s As String = CStr(ViewState("Text"))
            If s Is Nothing Then
                Return "[" + Me.ID + "]"
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("Text") = Value
        End Set
    End Property

    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
        output.RenderBeginTag(HtmlTextWriterTag.Div)

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

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

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

        output.RenderEndTag()

    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        If (Page.Request.Browser.EcmaScriptVersion.Major > 0) Then
            Page.ClientScript.RegisterStartupScript( _
                GetType(Page), "ClientCallback", _
                "function ClientCallback() {" & _
                    "args=document.getElementById('" & Me.ClientID & "_i" & "').value;" & _
                    Page.ClientScript.GetCallbackEventReference(Me, "args", _
                       "CallbackHandler", Nothing, "ErrorHandler", True) + "}", _
                True)

            Page.ClientScript.RegisterStartupScript(GetType(Page), _
                "ControlFocus", "document.getElementById('" & _
                Me.ClientID & "_i" & "').focus();", _
                True)
        End If
    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 System.Web.UI.ICallbackEventHandler.GetCallbackResult

        Return "Valid Data"
    End Function

End Class