2016-03-12 68 views

回答

1

只需將驗證器控件放置在要顯示消息的控件附近,即只要將驗證器控件放在那裏即可顯示消息。

在下面的示例中,我在靠近某個其他控件(不靠近文本框)附近顯示驗證消息。

<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> 
<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="Dynamic" 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> 
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="txtNameRequired" 
    Display="Dynamic" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator> 

<br /> 
<asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" /> 
<br /> 
</form> 

希望這可以幫助你。

相關問題