Change Text Box Color using Required Field Validator. No Extender Controls Please

前端 未结 16 2154
广开言路
广开言路 2020-11-28 07:07

I need to change color of TextBox whenever its required field validator is fired on Clicking the Submit button

16条回答
  •  旧巷少年郎
    2020-11-28 07:43

    You could use CustomValidator instead of RequiredFieldValidator:

    .ASPX

    
    
    
    
    
    
    

    .CS

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        bool is_valid = TextBox1.Text != "";
        TextBox1.BackColor = is_valid ? Color.White : Color.Red;
        args.IsValid = is_valid;
    }
    

    Logic in client and server validation functions is the same, but the client function uses jQuery to access textbox value and modify its background color.

提交回复
热议问题