Source view of COMPAREVALIDATOR.ASPX
DotNet Funda: Code Viewer
comparevalidator.aspx | comparevalidator.aspx.cs
Close Window  
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderHeader" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderForTitleAndIntro" Runat="Server">
    <table width="100%" cellpadding="2" cellspacing="0">
        <tr valign="top" class="ArticleTitle">
            <td style="padding-left:10px;" valign="middle">
                asp:CompareValidator control</td>
        </tr>
        <tr>
            <td class="ArticleContents">
                CompareValidator control is used to compare two values. The value to compare can be either a value of another control or a constant specified.
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderForContents" Runat="Server">
    <div class="ArticleContents">
        CompareValidator control is used to comapre two values. The value to compare can be either a value of another control or a constant specified.
        There are predefined data types that can be compared like string, integer etc.
        
        <p>&nbsp;</p>
        Following are main properties of the validation control.
        <table width="100%" class="TutoPropPlaceHolder" border="1" cellpadding="2" cellspacing="1">
            <tr>
                <td class="DemoCP">ControlToCompare</td>
                <td>
                    Gets or sets the ID of the control whose value will be compared with the currently entered value.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">Operator</td>
                <td>
                    DataTypeCheck/Equal/GreaterThan/GreaterThanEqual/LessThan/LessThanEqual/NotEqual.
                    Used to specify the comparison operation to peform. In case of DataTypeCheck, <span class="DemoCP">ControlToCompare</span> properties are ingnored.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">Display</td>
                <td>
                    Dynamic/Static. Used to indicate how the area of error message will be allocated. <br />
                    Dynamic: Error message area will only be allocated when error will be displayed. Static: Error messagea area will be allocated in either case.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">Enabled</td>
                <td>
                    true/false. Gets or sets whether to enable the validation control or not.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">ErrorMessage</td>
                <td>
                    Gets or sets the text of the error message that will be displayed when validation fails (This is displayed when ValidationSummary validatoin control is used.).
                </td>
            </tr>
            <tr>
                <td class="DemoCP">Text</td>
                <td>
                    Gets or sets the description of the error message text.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">ValidationGroup</td>
                <td>
                    Gets or sets the validation group it belongs to. This is used to group a set of controls.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">SetFocusOnError</td>
                <td>
                    true/false. Used to move focus on to the control that fails the validation.
                </td>
            </tr>
        </table>
        <!-- START - Demo Section -->
        <table class="DemoPlaceHolder" border="1" cellpadding="2" cellspacing="4">
            <tr>
                <td class="DemoTitle">
                    DEMO : CompareValidator
                </td>
                <td align="right">
                    <a class="DemoShowSource" href="../../misc/codeviewer/default.aspx?pagename=~/tutorials/controls/comparevalidator.aspx" target="_blank">Show Source Code</a>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lbl" AssociatedControlID="TextBox1" runat="Server" Text="Write into TextBox"></asp:Label>
                    <asp:TextBox ID="TextBox1" runat="Server"></asp:TextBox>
                    <asp:TextBox ID="TextBox2" runat="Server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="req1" runat="Server" ControlToValidate="TextBox1" ErrorMessage="1st TextBox is Mandatory field" Text="<br>Please write something in 1st  Box."></asp:RequiredFieldValidator>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="Server" ControlToValidate="TextBox2" ErrorMessage="2nd TextBox is Mandatory field" Text="<br>Please write something in 2nd Box."></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="CompareValidator1" runat="Server" ControlToValidate="TextBox2" ControlToCompare="TextBox1" Operator="Equal" Type="string" Text="Both textbox value should be same." ErrorMessage="Both textbox values are not equal." Display="Static"></asp:CompareValidator>
                    
                    <asp:Button ID="btnSubmit" runat="Server" OnClick="WriteTextBoxValue" Text="Submit" />                
                    <asp:ValidationSummary ID="ValidationSummary" runat="Server" ShowMessageBox="true" />
                </td>
                <td>
                    <asp:Label ID="lblMessage" runat="Server" ForeColor="red" EnableViewState="False"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <pre>    
&lt;asp:Label ID="lbl" AssociatedControlID="TextBox1" runat="Server" Text="Write into TextBox"&gt;&lt;/asp:Label&gt;
    &lt;asp:TextBox ID="TextBox1" runat="Server"&gt;&lt;/asp:TextBox&gt;
    &lt;asp:TextBox ID="TextBox2" runat="Server"&gt;&lt;/asp:TextBox&gt;
    &lt;asp:RequiredFieldValidator ID="req1" runat="Server" ControlToValidate="TextBox1" ErrorMessage="1st TextBox is Mandatory field" Text="&lt;br&gt;Please write something in 1st  Box."&gt;&lt;/asp:RequiredFieldValidator&gt;
    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="Server" ControlToValidate="TextBox2" ErrorMessage="2nd TextBox is Mandatory field" Text="&lt;br&gt;Please write something in 2nd Box."&gt;&lt;/asp:RequiredFieldValidator&gt;
    &lt;asp:CompareValidator ID="CompareValidator1" runat="Server" ControlToValidate="TextBox2" ControlToCompare="TextBox1" Operator="Equal" Type="string" Text="Both textbox value should be same." ErrorMessage="Both textbox values are not equal." Display="Dynamic"&gt;&lt;/asp:CompareValidator&gt;
    
    &lt;asp:Button ID="btnSubmit" runat="Server" OnClick="WriteTextBoxValue" Text="Submit" /&gt;                
    &lt;asp:ValidationSummary ID="ValidationSummary" runat="Server" ShowMessageBox="true" /&gt;

                    </pre>
                </td>
            </tr>
        </table>
        <!-- END - Demo Section -->
        </div>
    <br />
 
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderFooter" Runat="Server">
</asp:Content>

Go Top