使用RequiredFieldValidator的ValidationGroup与不验证(Requi

2019-07-30 01:09发布

这里是我的标记:

Name: 
<asp:TextBox ID="txtNewName" runat="server" ValidationGroup="NewDepartmentValidationGroup" />
<asp:RequiredFieldValidator ID="vldtxtNewName" runat="server" ControlToValidate="txtNewName"
    ErrorMessage="Required Field" /><br />
Description: 
<asp:TextBox ID="txtNewDescription" runat="server"          
    ValidationGroup="NewDepartmentValidationGroup"/>
<asp:RequiredFieldValidator ID="vldtxtNewDescription" runat="server" 
    ControlToValidate="txtNewDescription" ErrorMessage="Required Field" /><br />
<asp:Button ID="cmdCreate" runat="server" Text="Create"
     ValidationGroup="NewDepartmentValidationGroup" OnClick="cmdCreate_Click" />

当我删除属性的ValidationGroup,该行为是按预期和客户端代码警告说,为必填字段。

但是,当我指定的ValidationGroup(如上面的例子中提到的),我点击与文本框为空的按钮,客户端代码不执行任何操作,按钮点击事件触发和Page.IsValid等于真实,我的代码收益,相反,预计什么。

任何想法如何解决这一问题?

Answer 1:

你是在验证器丢失的验证组。

有没有必要指定上的控件验证组(文本框),而不是在指定要在其中有效数据被张贴的验证和按钮验证组!

试试这个:

    Name: 
<asp:TextBox ID="txtNewName" runat="server" />
<asp:RequiredFieldValidator ID="vldtxtNewName" runat="server"
     ControlToValidate="txtNewName" ValidationGroup="NewDepartmentValidationGroup" 
     ErrorMessage="Required Field" /><br />
Description: 
<asp:TextBox ID="txtNewDescription" runat="server" />
<asp:RequiredFieldValidator ID="vldtxtNewDescription" runat="server" 
    ControlToValidate="txtNewDescription" ErrorMessage="Required Field"
    ValidationGroup="NewDepartmentValidationGroup" /><br />
<asp:Button ID="cmdCreate" runat="server" Text="Create"
     ValidationGroup="NewDepartmentValidationGroup" OnClick="cmdCreate_Click" />


Answer 2:

与=的ValidationGroup“NewDepartmentValidationGroup”尝试在文本框中输入验证不

<asp:TextBox ID="txtNewName" runat="server"  />
        <asp:RequiredFieldValidator ID="vldtxtNewName" runat="server" ControlToValidate="txtNewName"  ValidationGroup="NewDepartmentValidationGroup"
            ErrorMessage="Required Field" /><br />
Description: 
<asp:TextBox ID="txtNewDescription" runat="server"          
    />

<asp:RequiredFieldValidator ID="vldtxtNewDescription" runat="server" 
    ControlToValidate="txtNewDescription" ErrorMessage="Required Field" ValidationGroup="NewDepartmentValidationGroup"/><br />

<asp:Button ID="cmdCreate" runat="server" Text="Create"
      ValidationGroup="NewDepartmentValidationGroup" OnClick="cmdCreate_Click" causesvalidation="true" />


文章来源: RequiredFieldValidator with ValidationGroup doesn't validate