这是我的标记:
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,我的代码继续执行,这与预期相反。
有什么办法解决这个问题吗?
发布于 2012-06-19 19:52:41
验证器上缺少验证组。
不需要在控件(文本框)上指定验证组,而是在验证器上指定验证组,并指定要在其上发布有效数据的按钮!
试试这个:
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-19 19:56:11
尝试在验证器中使用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" />https://stackoverflow.com/questions/11100230
复制相似问题