Imports Microsoft.VisualBasic
Imports System.Web.UI.WebControls

Namespace MikesControls
    Public Class MyControl3
        Inherits WebControl

        Private _cmd As String
        Private _typeName As String

        Public Property MySqlCommand() As String
            Get
                Return _cmd
            End Get
            Set(ByVal value As String)
                _cmd = value
            End Set
        End Property

        Public Property TypeName() As String
            Get
                Return _typeName
            End Get
            Set(ByVal value As String)
                _typeName = value
            End Set
        End Property

        Public Sub New()
            ' Do Nothing
            ' Warning: The constructor gets called before the Property values
            ' are read and set.
            '
        End Sub

        Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim dataSource As Object
            Dim srcType As Type
            Dim srcMethodInfo As System.Reflection.MethodInfo
            Dim result As Object

            srcType = Type.GetType(_typeName)
            dataSource = Activator.CreateInstance(srcType)

            srcMethodInfo = srcType.GetMethod(_cmd)
            result = srcMethodInfo.Invoke(dataSource, Nothing)

            ' To be honest we are creating a FAKE data source.
            ' When this control is added to a web page it is
            ' added to the Controls container of the Page or
            ' another control e.g. a GridView, etc.
            ' This container has its own Namespace, so we
            ' must search the parent container for the control
            ' with the ID="MikesText".
            ' Finally check the type of this control and if
            ' it is a TextBox, set its value.
            '
            Dim ctrl As Control

            ctrl = Me.Parent.FindControl("MikesText")
            If TypeOf ctrl Is TextBox Then
                Dim txtCtrl As TextBox
                txtCtrl = CType(ctrl, TextBox)
                txtCtrl.Text = CType(result, String)
            End If

            ' No need to render anything here
        End Sub


    End Class
End Namespace

