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

''' <summary>
''' <para>This validator checks for check box's checked value.</para>
''' <para>This is a dirrect conversion form the C# code avilable at <a href="http://aspnet.4guysfromrolla.com/articles/092006-1.aspx">4GuysFromRolla</a>.</para>
''' <para>NOTE: assign control to validate using code or you will have to type the name in ControlToValidate property. The check boxes do no appear in the drop down list.</para>
''' </summary>
''' <remarks>Thnaks to 4GuysFromRolla.</remarks>
Public Class CheckBoxValidator
    Inherits BaseValidator

    ''' <summary>
    ''' Gets or sets a value indicating whether check box must be checked or not.
    ''' </summary>
    ''' <value><c>true</c> if check box must be checked; otherwise, <c>false</c>.</value>
    <Description("Whether the CheckBox must be checked or unchecked to be considered valid."), Category("Behavior")> _
    Public Property MustBeChecked() As Boolean
        Get
            Dim o As Object = ViewState("MustBeChecked")
            If o Is Nothing Then
                Return True
            Else
                Return CBool(o)
            End If
        End Get
        Set(ByVal value As Boolean)
            ViewState("MustBeChecked") = value
        End Set
    End Property

    Private _ctrlToValidate As CheckBox = Nothing
    Protected ReadOnly Property CheckBoxToValidate() As CheckBox
        Get
            If _ctrlToValidate Is Nothing Then
                _ctrlToValidate = TryCast(FindControl(Me.ControlToValidate), CheckBox)
            End If

            Return _ctrlToValidate
        End Get
    End Property

    Protected Overrides Function ControlPropertiesValid() As Boolean
        ' Make sure ControlToValidate is set 
        If Me.ControlToValidate.Length = 0 Then
            Throw New HttpException(String.Format("The ControlToValidate property of '{0}' cannot be blank.", Me.ID))
        End If

        ' Ensure that the control being validated is a CheckBox 
        If CheckBoxToValidate Is Nothing Then
            Throw New HttpException(String.Format("The CheckBoxValidator can only validate controls of type CheckBox."))
        End If

        Return True
        ' if we reach here, everything checks out 
    End Function

    Protected Overrides Function EvaluateIsValid() As Boolean
        ' Make sure that the CheckBox is set as directed by MustBeChecked 
        Return CheckBoxToValidate.Checked = MustBeChecked
    End Function

    Protected Overrides Sub AddAttributesToRender(ByVal writer As HtmlTextWriter)
        MyBase.AddAttributesToRender(writer)

        ' Add the client-side code (if needed) 
        If Me.RenderUplevel Then
            ' Indicate the mustBeChecked value and the client-side function to used for evaluation 
            ' Use AddAttribute if Helpers.EnableLegacyRendering is true; otherwise, use expando attributes 
            If Helpers.EnableLegacyRendering() Then
                writer.AddAttribute("evaluationfunction", "CheckBoxValidatorEvaluateIsValid", False)
                writer.AddAttribute("mustBeChecked", IIf(MustBeChecked, "true", "false"), False)
            Else
                Me.Page.ClientScript.RegisterExpandoAttribute(Me.ClientID, "evaluationfunction", "CheckBoxValidatorEvaluateIsValid", False)
                Me.Page.ClientScript.RegisterExpandoAttribute(Me.ClientID, "mustBeChecked", IIf(MustBeChecked, "true", "false"), False)
            End If
        End If
    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
        MyBase.OnPreRender(e)

        ' Register the client-side function using WebResource.axd (if needed) 
        ' see: http://aspnet.4guysfromrolla.com/articles/080906-1.aspx 
        If Me.RenderUplevel AndAlso Me.Page IsNot Nothing AndAlso Not Me.Page.ClientScript.IsClientScriptIncludeRegistered(Me.GetType, "skmValidators") Then
            Me.Page.ClientScript.RegisterClientScriptInclude(Me.GetType, "skmValidators", Me.Page.ClientScript.GetWebResourceUrl(Me.GetType, "Mitul.Web.skmValidators.js"))
        End If

    End Sub
End Class
