Page 77 - DCAP312_WEB_TECHNOLOGIES_II
P. 77

Unit 4: Advanced Server Controls



                                                                                                  Notes
                           Checking to Make Sure Value Entered is of a Specific Data Type
                           Age:
                           <asp:TextBox id= “TextBox1” runat= “server” MaxLength= “3”>
                           </asp:TextBox>
                            
                           <asp:CompareValidator id= “CompareValidator1” runat= “server”

                           ErrorMessage= “You must enter a number”
                           ControlToValidate= “TextBox1” Type= “Integer”
                           Operator= “DataTypeCheck”></asp:CompareValidator>
                           In this example, the user must enter an integer in the text box; otherwise, the
                           CompareValidator server control fires and displays an error message on the
                           page. By giving the Type property of the CompareValidator server control a
                           value of Integer, we ensure that the value entered in the text box conforms
                           to this .NET Framework data type.
                           We also have the option of not only comparing values against specific data
                           types, but also ensuring that values are valid when compared against certain
                           constants.

                           Age:
                           <asp:TextBox id= “TextBox1” runat= “server”></asp:TextBox>
                            
                           <asp:CompareValidator id= “CompareValidator1” runat= “server”
                           Operator= “GreaterThan” ValueToCompare= “18”
                           ControlToValidate= “TextBox1” ErrorMessage= “You must be older than
                           18 to join” Type= “Integer”></asp:CompareValidator>


            In this case, a few checks are made next to any value that the user types in the text box. The
            initial is based on the Type possessions in the CompareValidator server organize. The Type
            property has the value of digit, therefore any value located in the text box requirements to be
            conventional to this .NET Framework data type. If the user enters a string into the text box, the
            form submission is invalid. The next property is the Operator property. This property has a
            value of Greater Than, meaning that for the form submission to be valid, the value entered in
            the text box has to be greater than the value that is assigned to the ValueToCompare property.
            For this CompareValidator server control, the ValueToCompare property has a value of 18. This
            means that the value entered in the text box on the page must be an integer greater than 18. If it
            does not meet these criteria, the value is considered invalid, and the CompareValidator server
            control displays an error message in the browser.

            In the circumstances shown in above example, we are not comparing two fields in the form;
            instead,  we  are  comparing  one  field  against  a  value  that  we  have  particular  in  cipher.  The
            Operator property can contain one of the following values. The Operator property can contain
            one of the following values:
               •  Equal
               •  Not Equal



                                             LOVELY PROFESSIONAL UNIVERSITY                                    71
   72   73   74   75   76   77   78   79   80   81   82