Search code examples
asp.netvb.netrequiredfieldvalidator

VB.net disable CompareValidator from code behind


Is it possible to disable CompareValidator / RequiredFieldValidator from code behind in VB.net?

I have 3 field (dropdown and two dated picker) and once the month from dropdown is selected I want to disable validation / required on datepicker inputs.

Any suggestions much appreciated!

Please note have already tried this, and it does not work:

Protected Sub DashbodropardMonthsDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DashbodropardMonthsDropDown.SelectedIndexChanged
        ' If Month is selected from DD menu
        Dim MonthDD As Integer = DashbodropardMonthsDropDown.SelectedValue

        ' If nothing selected or selected "--select" with value 0
        If MonthDD = 0 Then

            'Validate DatePicker
            CompareValidatorT1.Enabled = True
            RequiredFieldValidator2.Enabled = True
            RequiredFieldValidator1.Enabled = True

        Else

            'Do Not Validate DatePicker
            CompareValidatorT1.Enabled = False
            RequiredFieldValidator2.Enabled = False
            RequiredFieldValidator1.Enabled = False


        End If

    End Sub

Solution

  • You can enable or disable your Validators from code behind by doing something like this

    CompValidator.Enabled=false
    

    EDIT: Setting AutoPostBack=true for the DropDownList helped