How to use ValidationSummary control in asp.net


ValidationSummary web server control

ValidationSummary control allow us to display summary of all validation errors.

We can display validation errors summary inline of a web page or a message box or both by using ShowMessageBox and ShowSummary property value true or false.

We can display validation messages as bulleted list, single paragraph or only list based on DisplayMode.

We can set a header text for validation summary. asp.net validationsummary control have many properties to design the error messages text as like fore color, back color, border color, border style, border width, theme, skin and after all css class.

Validationsummary allow to summarize of all validation error messages from all validators in a single location.

This example show you how can we display all validation error messages as summary using validationsummary control. here we uses three text box and make them required field using requiredfieldvalidator control. when someone submit form without entering textboxes value, he got a validation error messages summary in one location as bulleted list.

C# Code

  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

       protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Full Name";
            Label2.Text = "Email";
            Label3.Text = "Password";

            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;

            RequiredFieldValidator1.ControlToValidate = TextBox1.ClientID;
            RequiredFieldValidator2.ControlToValidate = TextBox2.ClientID;
            RequiredFieldValidator3.ControlToValidate = TextBox3.ClientID;

            RequiredFieldValidator1.ForeColor = System.Drawing.Color.Red;
            RequiredFieldValidator2.ForeColor = System.Drawing.Color.Red;
            RequiredFieldValidator3.ForeColor = System.Drawing.Color.Red;

            RequiredFieldValidator1.Text = "*";
            RequiredFieldValidator2.Text = "*";
            RequiredFieldValidator3.Text = "*";

            RequiredFieldValidator1.ErrorMessage = "Input Full Name";
            RequiredFieldValidator2.ErrorMessage = "Input Email";
            RequiredFieldValidator3.ErrorMessage = "Input Passsword";


            ValidationSummary1.HeaderText = "Following Error Occurs:";

            ValidationSummary1.ForeColor = System.Drawing.Color.Red;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label4.Visible = true;
            Label5.Visible = true;
            Label6.Visible = true;

            Label4.ForeColor = System.Drawing.Color.DarkBlue;
            Label5.ForeColor = System.Drawing.Color.DarkBlue;
            Label6.ForeColor = System.Drawing.Color.DarkBlue;

            Label4.Text = "Name: <b style='color:greed'>" + TextBox1.Text+"</b>";
            Label5.Text = "Email Address: <b style='color:greed'>" + TextBox2.Text + "</b>";
            Label6.Text = "Password: <b style='color:greed'>" + TextBox3.Text + "</b>";
        }

    }
}

HTML Code

<%@ Page Language="C#" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
    
   protected void Button1_Click(object sender, System.EventArgs e) {  
        Label1.Text = "Name: " +  
            TextBox1.Text.ToString() +  
            "<br />Emmail Address: " +  
            TextBox2.Text.ToString() +  
            "<br />Password: " +  
            TextBox3.Text.ToString();  
    }  
</script>  
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="DarkBlue"></asp:Label>  
        <br />
        <asp:Label ID="Label2" runat="server" Text="Full Name" AssociatedControlID="TextBox1"></asp:Label>  
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
        <asp:RequiredFieldValidator  
             ID="RequiredFieldValidator1"  
             runat="server"  
             ControlToValidate="TextBox1"  
             ErrorMessage='Input Full Name'  
             EnableClientScript="true"  
             SetFocusOnError="true"  
             Text="*"  
             >  
        </asp:RequiredFieldValidator>  
        <br />  
  
        <asp:Label ID="Label3" runat="server" Text="Email" AssociatedControlID="TextBox2"></asp:Label>  
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
        <asp:RequiredFieldValidator  
             ID="RequiredFieldValidator2"  
             runat="server"  
             ControlToValidate="TextBox2"  
             ErrorMessage='Input Email'  
             EnableClientScript="true"  
             SetFocusOnError="true"  
             Text="*"  
             >  
        </asp:RequiredFieldValidator>  
        <br />  
          
        <asp:Label ID="Label4" runat="server" Text="Password" AssociatedControlID="TextBox3">  
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
        <asp:RequiredFieldValidator  
             ID="RequiredFieldValidator3"  
             runat="server"  
             ControlToValidate="TextBox3"  
             ErrorMessage='Input Password'  
             EnableClientScript="true"  
             SetFocusOnError="true"  
             Text="*"  
             >  
        </asp:RequiredFieldValidator>  
        <br />  
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  
        <br />  
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Following error occurs:" ShowMessageBox="false" DisplayMode="BulletList" ShowSummary="true" />
    
    </div>
    </form>
</body>
</html>
Output
How to use ValidationSummary control in asp.net.png
How to use ValidationSummary control in asp.net.png

Share this article on