7

这是我的标记:

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 等于 true 并且我的代码继续执行,与预期什么。

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

4

2 回答 2

17

您缺少验证器上的验证组。

无需在控件(文本框)上指定验证组,而是在验证器上指定验证组以及您希望在其上发布有效数据的按钮!

试试这个:

    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" />
于 2012-06-19T11:52:40.867 回答
5

在不在文本框中的验证器中尝试 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" />
于 2012-06-19T11:56:11.427 回答