CompareValidator Example in ASP.NET using C#

CompareValidator Example in ASP.NET using C#


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

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

CompareValidator Example in ASP.NET using C#

CompareValidator control in ASP.NET is used to compare the value between two controls. If the value is different between two controls then error message will be displayed. You can set the ErrorMessage property which you want to display. The another properties are ControlToValidate in which you can set the attached control instance and ControlToCompare property in which you can set the instance of another control.

CompareValidator.aspx (Design Page):

CompareValidator.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true"
CodeFile="CompareValidator.aspx.cs" Inherits="CompareValidator" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<h2 style="color: Green">
CompareValidator in ASP.NET 4 , C#</h2>
<br />
<table cellpadding="0" cellspacing="0" style="height: 121px; width: 295px">
<tr>
<td><strong>Password: </strong></td>
<td><asp:TextBox ID="passwordTextBox" runat="server" TextMode="Password" 
Font-Names="Verdana"></asp:TextBox></td>
</tr>
<tr>
<td><strong>Confirm Password: </strong></td>
<td><asp:TextBox ID="confirmpasswordTextBox" runat="server" 
TextMode="Password" Font-Names="Verdana"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2"><asp:CompareValidator ID="CompareValidator1" runat="server" 
ErrorMessage="Password don't match"
ControlToCompare="passwordTextBox" 
ControlToValidate="confirmpasswordTextBox"
ForeColor="Red"></asp:CompareValidator></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><asp:Button ID="submitButton" runat="server" Text="Submit" Width="76px" 
OnClick="submitButton_Click" /></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