Disabling/enabling requiredFieldValidators with javascript and server side

后端 未结 5 1942
北海茫月
北海茫月 2020-12-30 12:12

I have a drop down (dropdown2) that is required IF there is something in it but it\'s options data is driven by ajax from another dropdown (dropdown1) selec

5条回答
  •  失恋的感觉
    2020-12-30 12:23

    So I didn't get JonH answer to work, and the rest is only client side. So this is my solution:

    To disable a RequiredFieldValidator on the client side:

    ValidatorEnable(document.getElementById("rfv"), false);
    

    To disable a RequiredFieldValidator on the server side you can override the Validate() method like this:

    public override void Validate()
    {
        bool disableRfv = input_to_check <> 1;
        rfv.Enabled = disableRfv;
        base.Validate();
    }
    

    Or, in VBasic:

    Public Overrides Sub Validate()
        Dim disable_rfv As Boolean = input_to_check <> 1
        rfv.Enabled = disable_rfv
    
        MyBase.Validate()
    End Sub
    

提交回复
热议问题