ASP.NET
  Home arrow ASP.NET arrow Page 2 - How to Add Code and Validation Controls to...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

How to Add Code and Validation Controls to a Form
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 3
    2009-07-01

    Table of Contents:
  • How to Add Code and Validation Controls to a Form
  • How to use the required field validator
  • How to add code to a form
  • How to use page and control events

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How to Add Code and Validation Controls to a Form - How to use the required field validator


    (Page 2 of 4 )

    To use the required field validator, you set the properties shown in the table at the top of figure 2-14. These are the properties that are used by all the validators.

    To start, you associate the validation control with a specific input control on the form through its ControlToValidate property. Then, when the focus leaves the input control or the user clicks on a button whose CausesValidation property is set to True, the validator checks whether a value has been entered into the input control. If not, the message in the ErrorMessage property is displayed.

    When an error occurs, the Display property of the validation control determines how the message in the ErrorMessage property is displayed. When you use flow layout, Dynamic usually works the best for this property. If you use a validation summary control as explained in chapter 7, though, you can change this property to None.

    If you look at the aspx code in this figure, you can see how the properties are set for the two required field validators that are shown in the previous figure. The first one validates the text box named txtInterestRate. The second one validates the text box named txtYears. This aspx code will be added after the end tag for the table in the code in figure 2-12.

    How to use the range validator

    The range validator lets you set the valid range for an input value. To use this control, you set the properties in the first table in figure 2-14, plus the properties in the second table. In particular, you set the minimum and maximum values for an input value.

    For this control to work correctly, you must set the Type property to the type of data you’re testing for. Because the interest rate entry can have decimal positions, for example, the Type property for the first range validator is set to Double. In contrast, because the year entry should be a whole number, the Type property for the second range validator is set to Integer. You can see how all of the properties for the two range validators are set by reviewing the aspx code.

    Common validation control properties  

     

     

     

     

    Property

    Description

    ControlToValidate

    The ID of the control to be validated.

    Display

    Determines how an error message is displayed. Specify Static to allocate space for the message in the page layout, Dynamic to have the space allocated when an error occurs, or None to display the errors in a validation summary control.

    ErrorMessage

    The message that’s displayed in the validation control when the validation fails.

     

     

     

     

    Additional properties of a range validator  

     

     

     

     

    Property

    Description

    MaximumValue

    The maximum value that the control can contain.

    MinimumValue

    The minimum value that the control can contain.

    Type

    The data type to use for range checking (String, Integer, Double, Date, or Currency).

     

     

     

     

    The aspx code for the validation controls on the Future Value form

      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
          ControlToValidate="txtInterestRate" Display="Dynamic"
          ErrorMessage="Interest rate is required.">
      </asp:RequiredFieldValidator&nbsp;>
      <asp:RangeValidator ID="RangeValidator1" runat="server" 
          ControlToValidate="txtInterestRate" Display="Dynamic"
          ErrorMessage="Interest rate must range from 1 to 20."
          MaximumValue="20" MinimumValue="1" Type="Double">
     
    </asp:RangeValidator><br />
     
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
          ControlToValidate="txtYears" Display="Dynamic"
          ErrorMessage="Number of years is required.">
      </asp:RequiredFieldValidator>&nbsp;
      <asp:RangeValidator ID="RangeValidator2" runat="server"
          ControlToValidate="txtYears" Display="Dynamic"
          ErrorMessage="Years must range from 1 to 45."
          MaximumValue="45" MinimumValue="1" Type="Integer">
     
    </asp:RangeValidator>

    Description

    1. The required field validator is typically used with text box controls, but can also be used with list controls.
    2. The range validator tests whether a user entry falls within a valid range. 
       
    3. If the user doesn’t enter a value into the input control that a range validator is associated with, the range validation test passes. Because of that, you should also provide a required field validator if a value is required.

    --------------------------------------------Figure 2-14   How to use the required field and range validators

    More ASP.NET Articles
    More By Murach Publishing


     

    Buy this book now. This article is an excerpt from chapter two of Murach's ASP.NET 3.5 Web Programming with VB 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774472). Check it out today at your favorite bookstore. Buy this book now.

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek