RequiredFieldValidator

RequiredFieldValidator

如果在asp.net中的验证失败,我想将CssClass更改为txtbox300CommentRED。

我找不到方法可以做到这一点。如果验证失败,是否可以告诉RequiredFieldValidator或其他Validators更改txtFullName的CssClass类?

<asp:TextBox ID="txtFullName" runat="server" CssClass="txtbox300Comment"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>


我怎样才能做到这一点,什么是无忧无虑的专业方式。

感谢帮助。

最佳答案

ControlToValidate =“ txtFullName”在RequiredFieldValidator上设置

if(!RequiredFieldValidator1.IsValid){
    //You might have to adjust where its looking for the control
    TextBox txt = form1.FindControl(RequiredFieldValidator1.ControlToValidate) as TextBox;
    if (txt != null)
    {
        txt.CssClass = "txtbox300Comment";
    }

}

10-02 18:51