How to use ValidationSummary control in ASP.NET using VB.NET

In this article you will learn about ValidationSummary control,its property and how you can use this control.
  • 4481
 

Introduction
 
A ValidationSummary control is used to display a summary of all the validation errors occurred in a web page. The error message displayed in this control is specified by the Error message property of each validation control. A Validation control is displayed when the IsValid property of the page is false and if the Error message property of the validation control is not set, no error message is displayed for that validation control. If "Polls" each of the validation controls on the page and aggregates the Text messages exposed by each. It does this through the PageValidator collection. Each validation control is added to this collection. The validationSummary can use this to determines which validators have errors.
 
Properties of ValidationSummary control

  • Enabled:- A Boolean value that specifies whether the validation control is enabled or not. 
  • EnableClientScript:- A Boolean value that specifies whether the client side validation is enabled or not. 
  • ForeColor:- Determines the ForeColor of the control. 
  • HeaderText:- Displays a header in the ValidationSummary control. 
  • ShowSummary:- A Boolean value that specifies whether the ValidationSummary control should be displayed or hidden.
Getting Started
  • Simply create a new ASP.NET web application. 
  • Drag a table, two Textboxes, Label, Button and ValidationSummary control on page. The page will display like below.

    Validation1.gif
     
  • Then write the below code on the source mode of the page.

    <%@ Page Language="vb" %>
    <html>
    <
    head>
       <title>Validation Control Example</title>
        <script language="javascript">
        <!--
            function ClientValidate(source, arguments) {
                var r, re;     
                re = new RegExp(/^[1-9][0-9][0-9][0-9]$/); 
                r = re.test(arguments.Value); 
                arguments.IsValid = r;   
            }
       -->
       </script>
       <script runat="server">
           Sub Page_Load()
               vsSummary.DisplayMode = ValidationSummaryDisplayMode.List
           End Sub
           Sub ServerValidation(ByVal source As Object, ByVal args As ServerValidateEventArgs)
               Dim RegExVal As New System.Text.RegularExpressions.Regex("^\d{4}$")
               If RegExVal.IsMatch(args.Value) Then
                   args.IsValid = True
               Else
                   args.IsValid = False
               End If
           End Sub
           </script>
    </head>
    <
    body>
       <h1>Validation Control Example</h1>
       <form id="Form1" runat="server">
          <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
             <asp:tablerow ID="Tablerow1" runat="server">
                <asp:tablecell ID="Tablecell1" runat="server">
                   Compare Validator Control:
                   <br/><br/>
                   Enter two numbers to compare
                </asp:tablecell>
                <asp:tablecell ID="Tablecell2" runat="server">
                   <asp:textbox id="value1" runat="server"/><br/>
                   <asp:textbox id="value2" runat="server"/><br/>
                   <asp:comparevalidator id="cvCompare"
                      controltovalidate="value1"
                      controltocompare="value2"
                      operator="equal"
                      type="integer"
                      errormessage="Fields are not equal!"
                      display="dynamic"
                      runat="server"/>
                </asp:tablecell>
             </asp:tablerow>
             <asp:tablerow ID="Tablerow2" runat="server">
                <asp:tablecell ID="Tablecell3" runat="server">
                   ValidationSummary Control:
                </asp:tablecell>
                <asp:tablecell ID="Tablecell4" runat="server">
                   <asp:validationsummary id="vsSummary"
                      displaymode="bulletlist"
                      headertext="Page has the following errors: "
                      showsummary="true"
                      showmessagebox="false"
                      runat="server"/>
                </asp:tablecell>
             </asp:tablerow>
             <asp:tablerow ID="Tablerow3" runat="server">
                <asp:tablecell ID="Tablecell5" colspan="2" runat="server">
                   <asp:button ID="Button1" text="submit" runat="server"/>
                </asp:tablecell>
             </asp:tablerow>
          </asp:table>
          <asp:label id="MyLabel" runat="server"/>
       </form>
    </body>
    </
    html>
     
  • Now Run your application.

Output

Validation2.gif

Validation3.gif

Validation5.gif

Validation4.gif

Summary
In this article you learned that how to use the ValidationSummary control in ASP.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.