Share via


CompareValidator Control

Compares one control to another by using a specified comparison operator.

From the properties in the Properties table, you can choose either the ControlToCompare property or the ValueToCompare property as your comparison criteria. If both are present, the value in the ControlToCompare property is used.

Mobile Controls Syntax

Required properties, defaults, and code-featured elements are noted in bold type.

<mobile:CompareValidator
   runat="server"
   id="id"
   Font-Name="fontName"
   Font-Size="{NotSet|Normal|Small|Large}"
   Font-Bold="{NotSet|False|True}"
   Font-Italic=""{NotSet|False|True}"
   ForeColor="foregroundColor"
   BackColor="backgroundColor"
   Alignment="{NotSet|Left|Center|Right}"
   StyleReference="styleReference"
   Text="{errorText}"
   Wrapping="{NotSet|Wrap|NoWrap}"

   ControlToCompare="IdOfControl"
   ControlToValidate="IdOfTargetControl"
   Display="{None | Static | Dynamic}"
   ErrorMessage="ErrorTextForSummary"
   Operator="{DataTypeCheck|Equal|GreaterThan|
      GreaterThanEqual|LessThan|
      LessThanEqual|NotEqual}"
   Type="{Currency|Date|Double|Integer|String}"
   ValueToCompare="Value">
innerText
</mobile:CompareValidator>

Containment Rules

The following controls can contain a CompareValidator control.

Control Comments
System.Web.UI.MobileControls.Form Can contain any number of CompareValidator controls.
System.Web.UI.MobileControls.Panel Can contain any number of CompareValidator controls.

The CompareValidator control cannot contain any other controls.

Device Templates

None.

Device-Specific Behavior

When any validator is rendered, the device-specific nature of its rendering is exactly the same as the device-specific rendering of a Label control. However, the StyleReference property for a validator is initially set to Error. It will use the error style in the page Stylesheet control or, if there is none, it will use the system default style sheet.

Example

The following example uses a CompareValidator control, which compares a number that a user enters with the number 24. If the number entered is less than 24, it is validated.

<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage"
    Language="VB" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<script language="vb" runat="server">
protected Sub Submit_Click(sender As Object, e As EventArgs)
   If Page.IsValid Then
      ActiveForm = Form2
   End If
End Sub
</script>

<mobile:Form id="Form1" runat="server">
   <mobile:Label runat="server">
   Please enter an integer between 1 to 23
   </mobile:Label>

   <mobile:CompareValidator ControlToValidate="number"
      Type="Integer"
      Operator="LessThan"
      ValueToCompare="24"
      runat="server">
   Invalid number
   </mobile:CompareValidator>

   <mobile:TextBox id="number" runat="server"/>

   <mobile:Command runat="server"
      OnClick="Submit_Click">Submit</mobile:Command>
</mobile:Form>

<mobile:Form id="Form2" runat="server">
   <mobile:Label runat="server">number is submitted</mobile:Label>
   <mobile:Link runat="server" NavigateURL="#Form1" text="Return" />
</mobile:Form>
[C#]
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage"
    Language="C#" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<script language="c#" runat="server">
protected void Submit_Click(Object sender, EventArgs e)
{
   if (Page.IsValid)
   {
      ActiveForm = Form2;
   }
}
</script>

<mobile:Form id="Form1" runat="server">
   <mobile:Label runat="server">
   Please enter an integer between 1 to 23
   </mobile:Label>

   <mobile:CompareValidator ControlToValidate="number"
      Type="Integer"
      Operator="LessThan"
      ValueToCompare="24"
      runat="server">
   Invalid number
   </mobile:CompareValidator>

   <mobile:TextBox id="number" runat="server"/>

   <mobile:Command runat="server"
      OnClick="Submit_Click">Submit</mobile:Command>
</mobile:Form>

<mobile:Form id="Form2" runat="server">
   <mobile:Label runat="server">number is submitted</mobile:Label>
   <mobile:Link runat="server" NavigateURL="#Form1" text="Return" />
</mobile:Form>

See Also

CompareValidator Class | CompareValidator Class Members | CompareValidator Web Server Control | Control Reference