antonydoyle
11/17/2015 - 9:30 AM

Server control

Server control

Imports CMS_API.Utilities.controls

Public Class TextBoxWithRedirect
    Inherits BasePlaceHolderControl

#Region "Private Variables"
    Private redirectWrapper As HTMLHelper.Div
    Private redirectLabel As ContensisLabel
    Private redirectTextBox As System.Web.UI.webcontrols.TextBox
    Private WithEvents redirectButton As Button
    Private WithEvents redirectImageButton As System.Web.UI.WebControls.ImageButton

    Private _buttonText As String
    Private _labelText As String
    Private _DefaultTextboxText As String
    Private _redirectURL As String
    Private _ButtonImage As String
#End Region

#Region "Public Properties"

    Public Property ButtonImage() As String
        Get
            If _ButtonImage Is Nothing Then
                Return String.Empty
            End If
            Return _ButtonImage
        End Get
        Set(ByVal Value As String)
            _ButtonImage = Value
        End Set
    End Property
    Public Property ButtonText() As String
        Get
            If _buttonText Is Nothing Then
                Return "go"
            Else
                Return _buttonText
            End If
        End Get
        Set(ByVal Value As String)
            _buttonText = Value
        End Set
    End Property
    Public Property LabelText() As String
        Get
            If _labelText Is Nothing Then
                Return String.Empty
            Else
                Return _labelText
            End If
        End Get
        Set(ByVal Value As String)
            _labelText = Value
        End Set
    End Property
    Public Property RedirectURL() As String
        Get
            If _redirectURL Is Nothing Then
                Return "http://www.google.com/search?q={0}"
            Else
                Return _redirectURL
            End If
        End Get
        Set(ByVal Value As String)
            _redirectURL = Value
        End Set
    End Property

    Public Property DefaultTextboxText() As String
        Get
            If _DefaultTextboxText Is Nothing Then
                Return String.Empty
            Else
                Return _DefaultTextboxText
            End If
        End Get
        Set(ByVal Value As String)
            _DefaultTextboxText = Value
        End Set
    End Property
#End Region

    Private Sub TextBoxWithRedirect_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init
        Me.Controls.Clear()
        redirectWrapper = New HTMLHelper.Div
        redirectWrapper.CssClass = "sys_textBoxWithRedirect"
        redirectLabel = New ContensisLabel
        redirectTextBox = New System.Web.UI.webcontrols.TextBox
        redirectButton = New Button

        redirectImageButton = New System.Web.UI.WebControls.ImageButton
        redirectImageButton.ID = Me.ID & "_redirectImageButton"
        redirectImageButton.ImageUrl = Me.ButtonImage

        redirectTextBox.ID = Me.ID & "_redirectTextBox"
        redirectLabel.ID = Me.ID & "_redirectLabel"
        redirectButton.ID = Me.ID & "_redirectButton"

        redirectLabel.AssociatedControlID = redirectTextBox.ID
        redirectLabel.Text = LabelText
        redirectButton.Text = ButtonText

        redirectWrapper.Controls.Add(redirectLabel)
        redirectWrapper.Controls.Add(redirectTextBox)

        If Me.ButtonImage.Length > 0 Then
            redirectWrapper.Controls.Add(redirectImageButton)
            Me.ParentPage.Utilities.SubmitOnReturn(redirectTextBox, redirectImageButton)
        Else
            redirectWrapper.Controls.Add(redirectButton)
            Me.ParentPage.Utilities.SubmitOnReturn(redirectTextBox, redirectButton)
        End If

        If LabelText = String.Empty Then
            redirectLabel.Visible = False
        Else
            redirectLabel.Visible = True
        End If

        Me.Controls.Add(redirectWrapper)
    End Sub

    Private Sub redirectButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles redirectButton.Click
        If redirectTextBox.Text <> String.Empty Then
            Page.Response.StatusCode = 301
            Page.Response.Redirect(String.Format(RedirectURL, System.Web.HttpUtility.UrlEncode(redirectTextBox.Text)))
        End If
    End Sub

    Private Sub redirectImageButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles redirectImageButton.Click
        If redirectTextBox.Text <> String.Empty Then
            Page.Response.StatusCode = 301
            Page.Response.Redirect(String.Format(RedirectURL, System.Web.HttpUtility.UrlEncode(redirectTextBox.Text)))
        End If
    End Sub

    Private Sub TextBoxWithRedirect_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not _DefaultTextboxText Is Nothing AndAlso _DefaultTextboxText.Length > 0 Then
            redirectTextBox.Text = _DefaultTextboxText
            Me.ParentPage.Utilities.ClearTextOnFocus(redirectTextBox, _DefaultTextboxText)
        End If
    End Sub
End Class