Saturday 14 April 2012

CompareValidator Control in asp.net

Definition 

The CompareValidator control allows you to compare the value entered by the user into an input control such as TextBox,TextArea,etc..
syntax :
<asp:CompareValidator
     id="ProgrammaticID"
     ControlToValidate="Programmatic ID of Server Control to Validate"
     ValueToCompare="value"
     ControlToCompare="value"  
     Type="DataType" 
     Operator="Operator Value" 
     ErrorMessage="Message to display in ValidationSummary control"
     Text="Message to display in control"  
     ForeColor="value"
     BackColor="value"  
     runat="server" > 
</asp:CompareValidator>
Properties For Campare Validator:
Property
Description
BackColor
This Property is use to change the background color.
ControlToCompare
The name of the control which is going to compare with
ControlToValidate
The id of the control to validate
Display
The display behavior for the validation control. Legal values are:
          None – does not display the validation and only displayed in the          ASP.NET Validation Summary control.         Static – displays the validation message if check fails. The space of the control is reserved even if the check passes.          Dynamic – displays the validation message if check fails. The space of the control is not reserved if the check passes.
EnableClientScript
Boolean value to specify if client-side validation is enabled or disabled.
Enabled
Property to set a Boolean value to specify if validation control is enabled or disabled.
ErrorMessage
The text to display in the Validation Summary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set
ForeColor
The foreground color of the control
id
A unique id for the control
IsValid
A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid
Operator
The type of comparison to perform. The operators are:
  • NotEqual
  • DataTypeCheck
  • Equal
  • GreaterThan
  • GreaterThanEqual
  • LessThan
  • LessThanEqual
runat
Specifies that the control is a server control. Must be set to "server"
Text
The message to display when validation fails
Type
Specifies the data type of the values to compare. The types are:
  • Currency
  • Date
  • Double
  • Integer
  • String
ValueToCompare
A specified value to compare with
Example:
<form runat="server"> <table>    <tr valign="top">      <td colspan="4"><h4>Compare two values</h4></td>    </tr>     <tr valign="top">      <td><asp:TextBox id="txt01" runat="server" /></td>      <td> = </td>      <td><asp:TextBox id="txt02" runat="server" /></td>      <td><asp:Button Text="Validate" runat="server" /></td>    </tr> </table> <br /> <asp:CompareValidator id="compval"  Display="dynamic" ControlToValidate="txt01"  ControlToCompare="txt02"  ForeColor="red"  BackColor="yellow"  Type="String" EnableClientScript="false"  Text="Validation Failed!"  runat="server" /> </form>






No comments:

Post a Comment