解决方法
<form id="form1" runat="server"> <asp:Label ID="lblNamerequired" runat="server" Text="*Name :"></asp:Label> <asp:TextBox ID="txtNamerequired" runat="server" ValidationGroup="Validation"></asp:TextBox> <asp:requiredFieldValidator ID="requiredFieldValidatorName" runat="server" ControlToValidate="txtNamerequired" Display="None" ErrorMessage="Name is required" ValidationGroup="Validation"></asp:requiredFieldValidator> <br /> <asp:Label ID="lblGenderrequired" runat="server" Text="*Gender :"></asp:Label> <asp:DropDownList ID="ddlGenderrequired" runat="server" ValidationGroup="Validation"> <asp:ListItem Selected="True" Value="-1">--Select--</asp:ListItem> <asp:ListItem Value="0">Male</asp:ListItem> <asp:ListItem Value="1">Female</asp:ListItem> </asp:DropDownList> <asp:CompareValidator ID="CompareValidatorGender" runat="server" ControlToValidate="ddlGenderrequired" Display="None" ErrorMessage="Gender is required" Operator="NotEqual" ValidationGroup="Validation" ValueToCompare="-1"></asp:CompareValidator> <br /> <asp:Label ID="lblValidation" runat="server" Text="Fields marked with * are required"></asp:Label> <br /> <asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" /> <br /> <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" ShowSummary="False" ValidationGroup="Validation" /> </form>
您应该在所有验证控件上使用相同的ValidationGroup文本,并使用ValidationGroup和ShowMessageBox =“true”添加ValidationSummary