简体   繁体   中英

Show ValidationErrors in Validation Summary only in asp.net

<asp:ValidationSummary ID="vsArea" runat="server" />
<div class="controls">
    <asp:DropDownList ID="ddlDegreeLevel" runat="server" 
                      placeholder="DegreeLevel" CssClass="form-control">
    </asp:DropDownList>
    <asp:RequiredFieldValidator ID="rfvDegreeLevel" runat="server"
                                ControlToValidate="ddlDegreeLevel"
                                Display="None" ForeColor="Red" InitialValue="-1"
                                ErrorMessage="Please select degree level" Text="">
    </asp:RequiredFieldValidator>
</div>

Errormessage of "rfvDegreeLevel" is show on validation-Summary and also below the control. I also set Display="None" but still show on both area .

I want to show message only on Validation summary.

This is by design...message will be shown in validation control and in the summary. Basically you have 2 options

  1. Show separate messages in your control and validation summary. Eg you can put a red * in the validation control and detailed error message in the summary, which is a best practice. To achieve this, set both Text and ErrorMessage property of the validation control. Text will be appearing in the control and ErrorMessage will be appearing in the summary.

  2. hide the message from control, set the Display property of the control to none..

You have to add validationgroup to all the controls. For this you can use the following code:

<asp:ValidationSummary ID="vsArea" runat="server" ValidationGroup="degree" />
<div class="controls">
    <asp:DropDownList ID="ddlDegreeLevel" runat="server" 
                      placeholder="DegreeLevel" CssClass="form-control">
    </asp:DropDownList>
    <asp:RequiredFieldValidator ID="rfvDegreeLevel" runat="server"  
                                ValidationGroup="degree"
                                ControlToValidate="ddlDegreeLevel"
                                Display="None" ForeColor="Red" InitialValue="-1"
                                ErrorMessage="Please select degree level" Text="">
    </asp:RequiredFieldValidator>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM