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

前端 未结 16 2180
广开言路
广开言路 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

    I too liked Alexanders and Steves answer but I wanted the same as in codebehind. I think this code might do it but it differes depending on your setup. My controls are inside a contentplaceholder.

    protected void cvPhone_ServerValidate(object source, ServerValidateEventArgs args)
    {
        bool is_valid = !string.IsNullOrEmpty(args.Value);
        string control = ((CustomValidator)source).ControlToValidate;
        ((TextBox)this.Master.FindControl("ContentBody").FindControl(control)).CssClass = is_valid ? string.Empty : "inputError";
        args.IsValid = is_valid;
    }
    

提交回复
热议问题