Thursday 26 July 2012

ValidationSummary in ASP.NET | How to use ValidationSummary in ASP.NET | What is the use of ValidationSummary in ASP.NET

Hi friends, in this article I will explain about How to use ValidationSummary control in ASP.NET, Custom Validation Summary in ASP.NET.
The ValidationSummary control is used to display a summary of all validation errors occurred in a Web page.
The error message displayed in this control is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed for that validation control.
I will explain ValidationSummary one with small example.
Create one website (file---new website)
Take one web page and name it as valsum.aspx
Write the below code in .aspx file
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="valsum.aspx.vb" Inherits="valsum" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <title>Aspdotnet-kishore:ValidationSummary example</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <h1 style ="color :red">Aspdotnet-kishore:ValidationSummary example</h1>
          <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="green"></asp:Label>
     <asp:Label ID="Label2" runat="server" Text="User Name" AssociatedControlID="uname"></asp:Label> 
        <asp:TextBox ID="uname" runat="server"></asp:TextBox> 
        <asp:RequiredFieldValidator 
             ID="RequiredFieldValidator1" 
             runat="server" 
             ControlToValidate="uname" 
             ErrorMessage='Pls Enter your User name!' 
             EnableClientScript="true" 
             SetFocusOnError="true" 
             Text="*" 
             > 
        </asp:RequiredFieldValidator>    <br />          
        <asp:Label ID="Label3" runat="server" Text="Year of Birth" AssociatedControlID="dob"></asp:Label> 
        <asp:TextBox ID="dob" runat="server"></asp:TextBox> 
        <asp:RangeValidator ID="RangeValidator1"
        ControlToValidate="dob"
        MinimumValue="1985"
        MaximumValue="2000"
        type="integer"
        errormessage="The year must be between 1985 and 2000!"
        text="*"
        runat="server" />
        <br />   
        <asp:Label ID="Label4" runat="server" Text="Mobile Number" AssociatedControlID="mobile"></asp:Label> 
        <asp:TextBox ID="mobile" runat="server"></asp:TextBox> 
       <asp:RegularExpressionValidator ID="RegularExpressionValidator5"
        runat="server" Font-Size="10px"
        ControlToValidate="mobile"
        Display="Dynamic"
        ErrorMessage="Telephone Number must be 10-11 digit"
        ValidationExpression="^[0-9]{10,11}$"
        text="*">
        </asp:RegularExpressionValidator>
        <br />           
      
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /> 
        <br /><br /> 
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Following error occurs:" ShowMessageBox="false" DisplayMode="BulletList" ShowSummary="true" /> 
    </div> 
    </form> 
</body> 
</html>
In the above example I also explain about RequiredFieldValidator,RangeValidator,RegularExpressionValidator
And  in the code behind write the below code.
In VB.NET:
Partial Class valsum
    Inherits System.Web.UI.Page
    Protected Sub check_username(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
        If Len(e.Value) < 8 Or Len(e.Value) > 16 Then
            e.IsValid = False
        Else
            e.IsValid = True
        End If
    End Sub
End Class
In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

partial class valsum : System.Web.UI.Page
{
                protected void check_username(object sender, ServerValidateEventArgs e)
                {
                                if (Strings.Len(e.Value) < 8 | Strings.Len(e.Value) > 16) {
                                                e.IsValid = false;
                                } else {
                                                e.IsValid = true;
                                }
                }
}

Run the above program it will like below.
ValidationSummary  in ASP.NET | How to use ValidationSummary  in ASP.NET | What is the use of ValidationSummary  in ASP.NET

 The validation summary control will take of the above three textboxes. If  the error raise then  the error will display in validation summary control.
In previous articles i explained about validation controls in ASP.NET and how to use the validation controls in ASP.NET.
CompareValidator in ASP.NET
CustomValidator Control, How to Validate with a Custom Function for ASP.NET Server Controls, ASP.NET CustomValidator Control.
RegularExpressionValidator for Email ID in ASP.NET or ASP.NET RegularExpressionValidator example.
ValidationSummary in ASP.NET

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.