ASP.NET ValidationSummary Control: Centralizing and Displaying Form Validation Errors

Learn how to use the ASP.NET ValidationSummary control to efficiently display all validation errors from your web forms in a single, organized location. This tutorial explains its key properties for customization (display mode, header text, etc.) and demonstrates its use for creating user-friendly and informative error feedback.



ASP.NET ValidationSummary Control

What is ValidationSummary?

The ValidationSummary control in ASP.NET provides a convenient way to display all validation errors from your form in a single, centralized location. Instead of having error messages scattered throughout the page, they're neatly collected together.

ValidationSummary Properties

The ValidationSummary control has several properties you can use to customize its appearance and behavior:

  • DisplayMode: Controls how error messages are displayed (e.g., as a list, bullet points, or a single paragraph).
  • ShowMessageBox: Displays a pop-up message box with the errors (works best in modern browsers).
  • ShowSummary: Determines whether the summary is displayed or not.
  • ShowValidationErrors: Determines whether validation errors are shown.
  • Other properties control appearance (e.g., BackColor, ForeColor, Font, ToolTip).

Example: ValidationSummary

This example shows how to use ValidationSummary to collect and display error messages for a simple login form.

ValidationSummeryDemo.aspx (Example Code):

Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidationSummeryDemo.aspx.cs"
    Inherits="asp.netexample.ValidationSummeryDemo" %>

<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="BulletList" /><br />

User Name: <asp:TextBox ID="txtName" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
    ControlToValidate="txtName" ErrorMessage="User Name is required!" ForeColor="Red"></asp:RequiredFieldValidator><br />

Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
    ControlToValidate="txtPassword" ErrorMessage="Password is required!" ForeColor="Red"></asp:RequiredFieldValidator><br />

This code includes a ValidationSummary control set to display errors as a bulleted list. The RequiredFieldValidator controls will report errors to this summary.