ValidationSummary Example in ASP.NET using C#

ValidationSummary Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 30, 2010 at 6:23 PM Comments : [ 0 ]

In this article we will introduce with the ValidationSummary control in ASP.NET using C#.

ValidationSummary Example in ASP.NET using C#

ValidationSummary control in ASP.NET is used to display the error messages in a place. If we use so many validation control in the single page then all the error message will be displayed in ValidationSummary. In this example we use so many validator when we click the Button all the error message will be displayed in ValidationSummary.

ValidationSummary.aspx (Design Page):

ValidationSummary.aspx (source page):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true"
CodeFile="ValidationSummary.aspx.cs" Inherits="ValidationSummary" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<script language="javascript" type="text/javascript">
function JSValidate(source, args) {
var element = document.getElementById('<%=TextBox1.ClientID %>');
if (element.value.length >= 6 && element.value.length <= 15) 
{
args.IsValid = true;
}
else {
args.IsValid = false;
}
}
</script>
<style type="text/css">
.style2
{
font-family: Verdana;
font-weight: bold;
font-size: small;
width: 142px;
}
.style3
{
width: 142px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<h2 style="color: Green">
ValidationSummary in ASP.NET 4 , C#</h2>
<br />
<table style="height: 329px; width: 546px">
<tr>
<td class="style2">User Name: </td>
<td><asp:TextBox ID="TextBox1" runat="server" Height="22px" Width="149px" 
Font-Names="Verdana"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
ErrorMessage="Please enter user name."
ControlToValidate="TextBox1" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<asp:CustomValidator ID="CustomValidator1" runat="server" 
ErrorMessage="The length between 6 to 15 character."
ControlToValidate="TextBox1" ForeColor="Red" 
ClientValidationFunction="JSValidate"
ValidateEmptyText="True"></asp:CustomValidator></td>
</tr>
<tr>
<td class="style2">Password: </td>
<td><asp:TextBox ID="TextBox2" runat="server" Height="22px" TextMode="Password" 
Width="149px" Font-Names="Verdana"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
ErrorMessage="Please enter password" ControlToValidate="TextBox2" ForeColor="Red">
</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td class="style2">Confirm Password: </td>
<td><asp:TextBox ID="TextBox3" runat="server" Height="22px" TextMode="Password"
Width="149px" Font-Names="Verdana"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
ErrorMessage="Please enter confirm password."ControlToValidate="TextBox3" ForeColor="Red">
</asp:RequiredFieldValidator><br />
<asp:CompareValidator ID="CompareValidator1" runat="server" 
ErrorMessage="Password don't match" ControlToCompare="TextBox2" ControlToValidate="TextBox3" 
ForeColor="Red"></asp:CompareValidator></td>
</tr>
<tr>
<td class="style2">Age: </td>
<td><asp:TextBox ID="TextBox4" runat="server" Height="22px" Width="149px" 
Font-Names="Verdana"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ErrorMessage="Please enter your age."
ControlToValidate="TextBox4" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<asp:RangeValidator ID="RangeValidator1" runat="server" 
ErrorMessage="Enter age between 30 and 60"
ControlToValidate="TextBox4" ForeColor="Red" MaximumValue="60" 
MinimumValue="30"
Type="Integer"></asp:RangeValidator></td>
</tr>
<tr>
<td class="style2">Email-id: </td>
<td><asp:TextBox ID="TextBox5" runat="server" Height="22px" Width="149px" 
Font-Names="Verdana"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
ErrorMessage="Please enter email-id."ControlToValidate="TextBox5" ForeColor="Red" Font-Size="Medium">
</asp:RequiredFieldValidator>
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
ErrorMessage="You must enter an email address" ControlToValidate="TextBox5" ForeColor="Red" 
ValidationExpression="[\w]+@[\w]+\.(com|net|org|co\.th|go\.th|ac\.th|or\.th|go\.th)">
</asp:RegularExpressionValidator></td>
</tr>
<tr>
<td class="style3">&nbsp;</td><td>
<asp:Button ID="submitButton" runat="server" Height="24px" Text="Submit" Width="80px"
OnClick="submitButton_Click" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="style3">&nbsp;</td><td colspan="2">
<asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#000099">
</asp:Label></td>
</tr>
<tr>
<td colspan="3"><asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" 
HeaderText="You received the following error!" Width="412px" /></td>
</tr>
</table>
</div>
</asp:Content>

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics