How to use ASP.NET CompareValidator Control

In this tutorial you will learn what is and how to use ASP.NET CompareValidator Control. ASP.NET CompareValidator Control is used to compare a value input to another input value or a fixed value. The following are a few things you should keep in mind when using ASP.NET CompareValidator Control:

  • CompareValidator Control can show successful comparison even if the input was empty.
  • It is a good practice to use the ASP.NET RequiredFieldValidator control to make sure the Input control field is a required field.

Adding ASP.NET CompareValidator Control

The following shows you step by step on how to add an ASP.NET CompareValidator Control in Visual Studio 2010.

  1. Open Visual Studio 2010 from the Start menu –> All Programs
  2. Go to the Toolbox and under the Validation section, select CompareValidator.

ASP.NET CompareValidator Control

ASP.NET CompareValidator Control

  1. Drag and drop it on to the Form area.
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator"></asp:CompareValidator>
  1. The following is shows an example use of the ASP.NET CompareValidator Control.
<form id="form1" runat="server">
     <div>
     <p>Comparing Values</p>
     <asp:TextBox id="TextBox1" runat="server" />
     <span>=</span>
     <asp:TextBox id="TextBox2" runat="server" />
     <asp:Button ID="Button2" Text="Validate" runat="server" />
     <br />
     <asp:CompareValidator id="CompareValidator1" Display="dynamic" ControlToValidate="TextBox1" ControlToCompare="TextBox2" ForeColor="red" BackColor="white" Type="String" EnableClientScript="false" Text="Comparision Failed" runat="server" />
     </div>
 </form>
  1. In the above code snippet, two textbox controls, a button control named “Validate” and one CompareValidator control was created.
  2. If the validation fails, the validation text “Comparison Failed” will be displayed in Red by the CompareValidator Control.

ASP.NET CompareValidator Control properties

The following table contains some of the important attributes and properties used together with the ASP.NET CompareValidator Control.

PropertyDescription
“BackColor”Property to set the background color for the Control
“ControlToCompare”Property to set the name of the ASP.NET Control to Compare to
“ControlToValidate”Property to set the id of the ASP.NET Control to validate
“Display”Property to set the display behavior for the ASP.NET validation control.

  • “None”– does not display the validation and only displayed in the ASP.NET ValidationSummary 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”Property to set a 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”Property to set the Validation text to be displayed in the ValidationSummary Control when the validation check fails. Even if the text property is not set, the default text will be displayed in the validation control.
“ForeColor”To set the foreground color of the ASP.NET CompareValidator Control
“id”A unique ID for the ASP.NET CompareValidator Control
“IsValid”Property to set a Boolean value to indicate if the control specified by ControlToValidate is determined to be valid.
“Operator”To set the type of comparison to perform:

  • “Equal”
  • “GreaterThan”
  • “GreaterThanEqual”
  • “LessThan”
  • “LessThanEqual”
  • “NotEqual”
  • “DataTypeCheck”
“runat”To indicate that the Control is a server control (by setting it to “”server””)
“Text”To set the validation text message to be displayed when check fails.
“Type”To set the data type of the values to compare:

  • “Currency”
  • “Date”
  • “Double”
  • “Integer”
  • “String”
“ValueToCompare”To set the value to compare with.

Referenced from: ASP.NET CompareValidator Control Properties

Posted in ASP.NET Tutorial | Tagged , , , | 1 Comment

What are ASP.NET Validation Server Controls

User input validation can be done with ease with the help of ASP.NET Validation Sever Controls. Coding is not required at all because ready-made ASP.NET Validation Server Controls are present for use with just a drag and drop away.

ASP.NET Validation Server Controls are used to validate the input data through a form by a user of the web application.  ASP.NET Validation Server Controls then checks if the input data by the user is validated and displays an error message if the validation fails.

Shown below is a code snipper of  ASP.NET Validation Server Controls.

<asp:control_name id=”some_id” runat=”server” />

ASP.NET Validation Server Controls
ASP.NET Validation Server Controls

  • CompareValidator - takes an input value and then compares it with another input value or a fixed value
  • CustomValidator - used to code your own validation methods to handle the data inputted by the user
  • RangeValidator - checks a value input by a user against a range i.e. whether it is between two values
  • RegularExpressionValidator - makes sure that the input value matches an already specified pattern
  • RequiredFieldValidator - makes an input field a required field
  • ValidationSummary - displays a report i.e. a summary of all validation errors occurred in a Web Page
Posted in ASP.NET 4 Theory | Tagged , , , , , | Leave a comment

ASP.NET Button Control

When developing an ASP.NET Web application using Visual Studio 2010, you can use an ASP.NET Button Control to display a Push button. These ASP.NET Buttons controls can be used as a Submit button or even an Command button.

ASP.NET Button Control do not have command name thus it posts the Web page back to the server when then control is clicked. When using the control an event handler can be coded to control the actions performed when the submit button is clicked.

Note:

  • ASP.NET Button Control is a Submit button by default.
  • ASP.NET Button control as a Command button has a command name which allows you to create multiple Button Controls on a page.
  • By codng an Event Handler, the ASP.NET Button control as a Command button’s actions performanced can be controlled.

Here are a few quick steps on how to add your very own ASP.NET Button Control.

  1. Go to the Toolbox and under the Standard section, select Button Control.

ASP.NET Button Control
ASP.NET Button Control

  1. Drag and drop it onto the Form area to declare it.
  2. The following is the code you get in the Source View.

<asp:Button ID=”Button1″ runat=”server” Text=”Button” />

Posted in ASP.NET Tutorial | Tagged , , , , , , | Leave a comment

ASP.NET BulletedList Control

When using ASP.NET to create web application, you have the ability to use ASP.NET BulletedList Control to create a bulleted list format for an ASP.NET .aspx web page. This control was first introduced in ASP.NET 2.0. When using ASP.NET BulletedList Control every item in the control is defined by a ListItem Element.

Here is a quick tutorial on adding your very own ASP.NET BulletedList Control.

  1. Go to the Toolbox and under the Standard section, select BulletedList Control.

ASP.NET BulletedList Control
ASP.NET BulletedList Control

  1. Drag and drop it onto the Form area to declare it.
  2. The following is the code excerpt you get in the Source View.

<asp:BulletedList ID=”BulletedList1″ runat=”server”>
</asp:BulletedList>

For more on how to use ASP.NET BulletedList Control check out the examples at www.aspnetbook.com

Posted in ASP.NET Tutorial | Tagged , , , , , | Leave a comment

ASP.NET Web Server Controls

When using ASP.NET, the ASP.NET Server interprets special tags known as ASP.NET WebServer Controls. These ASP.NET WebServer Controls like HTML Server Controls are interpreted at the Server end thus a runat=”server” tag is necessary. ASP.NET WebServer Controls also do not map to any current HTML elements and they also may represent more complex elements.

The following is a code snippet on how to Create a Web Server Control in ASP.NET.

<asp:control_name id=”some_id” runat=”server” />

ASP.NET Web Server Controls
ASP.NET Web Server Controls

Posted in ASP.NET 4 Theory | Tagged , , , , , | Leave a comment