ASP.NET
  Home arrow ASP.NET arrow Page 3 - Input Validation With ASP.NET, Part 1
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

Input Validation With ASP.NET, Part 1
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 36
    2004-02-16

    Table of Contents:
  • Input Validation With ASP.NET, Part 1
  • Making Friends
  • First Glance
  • If Looks Could Kill
  • Pizza Power
  • A Comparative Study

  • 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


    Input Validation With ASP.NET, Part 1 - First Glance


    (Page 3 of 6 )

    Enough with the theory, already; let's get our hands dirty with some code, shall we?


    <%@ Page Language="C#" %>
    <html>
    <head>
    <title>What's In A Name?</title>
    <basefont face="Arial">
    </head>
    <body>
    <form runat="server" method="POST" >
     
    <asp:label id="lblFirstName" runat="server" text="Your name, please? " />
    <asp:textbox id="strFirstName" runat="server"/>
    <br />
     
    <asp:RequiredFieldValidator id="strFirstNameRFV"
    ControlToValidate="strFirstName" ErrorMessage="Don'
    t you have a name?"
    runat="
    server"/>
    <br />
     
    <asp:button id="
    Submit" Text="Submit" runat="server"/>
    </form>
    </body>
    </html>

    In case you see errors about a missing "WebUIValidation.js" file instead of the form above, ensure that this file is under your Web server root, in the "\aspnet_client\system_web\1_1_4322" folder (you can locate the file by doing a quick search on your .NET development system), and then try again.

    Now, submit the form without entering any data, and you should now see an error message on the screen.

    There's nothing very exciting about the code above; the first few lines simply make use of two plain-vanilla server controls, a label and a textbox; the latter allows the user to enter his/her name. The meat of the script lies in this line:


    <asp:RequiredFieldValidator id="strFirstNameRFV"
    ControlToValidate
    ="strFirstName" ErrorMessage="Don't you have a name?"
    runat
    ="server"/>

    This is your very first ASP.NET Validator control. Aptly called the RequiredFieldValidator control, it forces the user to enter a value in the associated field. Here are the attributes I've used:

    1. The id attribute specifies a unique identifier for this control (useful if you want to script actions based on the state of this object).

    2. The ControlToValidate attribute associates this control with a form field. Since I want to validate the "strFirstName" textbox control, I've assigned it this value.

    3. The ErrorMessage attribute stores the message to be displayed to the user, if she does not enter a value in the "strFirstName" textbox control.

    4. The mandatory runat=server attribute tells the .NET CLR that this control has to be processed at the server and not the client.

    Now, you might be wondering whether the client or the server performed the validation. If you guessed the client, then you guessed right! Note that this doesn't mean that server-side validation is disabled; if, for some reason, client-side validation fails, the server will leap in and enforce your validations instead.

    More ASP.NET Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT