Skip to main content

How to use CompareValidator control in

CompareValidator control compare user inputted value with the another input control value or a constant value. comparevalidator validation success if inputted value matches the criteria specified by the Operator, ValueToCompare or ControlToCompare property. comparevalidator Type property indicate whether inputted value need to converted to specified data type (string, date, currency, double or integer). ControlToValidate property indicate which control need to validate. ControlToCompare property indicate which control need to compare with inputted value (such as TextBox value). ValueToCompare property indicate that you want to compare inputted value with a constant value instead other control value. Don't put ControlToValidate and ValueToCompare properties together in a comparevalidator control. you can use any one property at a time. that means you can only compare with a control or compare with a constant value at the same time.

you can use the Operator property to specify the type of comparison to perform, such as equal to, greater than, equal, not equal, less than, less than equal, data type check etc. Operator property value DataTypeCheck validated the data type specified by the Type property. so the comparevalidator validation fails if the inputted value cannot be converted to the specified data type.

this example demonstrate you how can we compare (validate) to password field (textbox) using comparevalidator control. if user inputted password not matches in both textboxes then validation fails and browser show a validation error message. if both textbox text matches then validation success.

<%@ Page Language="C#" %>
<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender,System.EventArgs e){
        Label1.Text = "Form Submited and Password match.";

<html xmlns="">
    <head id="Head1" runat="server">
        <title>CompareValidator example: how to use CompareValidator control in</title>
        <form id="form1" runat="server">
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <br />

                <asp:Label ID="Label2" runat="server" Text="<u>P</u>assword" AccessKey="P" AssociatedControlID="TextBox1"></asp:Label>
                <asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Text="*"></asp:RequiredFieldValidator>
                <asp:CompareValidator ID="CompareValidator" runat="server" ControlToValidate="TextBox1" ControlToCompare="TextBox2" ErrorMessage="Password does not match!">
                <br />

                <asp:Label ID="Label3" runat="server" Text="Re-Type Password" AssociatedControlID="TextBox2"></asp:Label>
                <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Compare" OnClick="Button1_Click" />
More example

Popular posts from this blog

How to use Table in

Table Web Server Control ASP.NET Table web server control display a table on a web page. It is a .NET web server control so we can program it in server code such as add, remove table row, cell programmatically. TableRow and TableCell web server controls allow us to display content for the Table. Table server control display tabular data and we can format Table server control using it's built in methods and properties. The big difference between HTML table and Table server control is that we can manipulate Table server control using an object model. Generally we should use Table server control when we intend to add remove rows and cells (columns) programmatically at run time.

Table server control acts as a parent (container) for TableRows controls. Table control's Rows property is a collection of TableRow objects. each TableRow control has a collection named Cells. Cells collection contains TableCell objects.

Actually table server control display the TableCell con…

How to use RadioButton control in c#

RadioButton web server control RadioButton control lets you make a group of radio buttons with other RadioButton. If you set it's GroupName property same for multiple radio buttons then all radio buttons with same name act as a single group. Within a group you can only select one RadioButton at a time. RadioButton group work as like RadioButtonList. With RadioButton control you can more customize it than RadioButtonList items.

Back color, border color, border style, font names, font bold, font italic, fore color (text color) and many more property help you to design RadioButton. It also support CSS class, theme and skin. Even validation are works with RadioButton control. RadioButton have AutoPostBack property and CheckChanged event. By using those feature you can determine immediately which RadioButton is selected from a group.

This example uses a RadioButton group with two RadioButton. Here we assign AutoPostBack property value to true and setup a CheckChanged event. When …

How to use RadioButtonList control in c#

RadioButtonList web server control ASP.NET RadioButtonList control enable user to select an item from list. RadioButtonList support data bind programmatically from database. We can also populate it manually by input list item inside RadioButtonList tag. RadioButtonList is a single selection radio button group. RadioButtonList have an items collection. We can determine which item is selected by test it's SelectedItem property.

We can change RadioButtonList design by setting up it's various property as like BackColor, BorderColor, BorderStyle, BorderWidth, CellPadding, CellSpacing, CssClass, Font-Bold, Font-Italic, Font-Names, Font-Overline, Font-Size etc. there are more three properties RepeatColumns, RepeatDirection and RepeatLayout which help you to place list item vertically or horizontally.

RadioButtonList have standard AutoPostBack property. As like other list control RadioButtonList have an excellent event SelectedIndexChanged. So that when someone select an item it…