.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Validate data in Silverlight

Posted By:Diptimaya Patra       Posted Date: March 18, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

Data Validation, in an ASP.NET site you can easily implement the Data Validation using the Validation options like Required Field Validator, Range Validator so and so forth. So the basic question that comes to our mind is that can we achieve that in Silverlight 3
 

Introduction

Data Validation, in an ASP.NET site you can easily implement the Data Validation using the Validation options like Required Field Validator, Range Validator so and so forth. So the basic question that comes to our mind is that can we achieve that in Silverlight 3. The answer is yes. In this article you will se how we can validate the user input.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a Silverlight Application. Name it as DataValidationSL3.

1.gif
 
To make the application look good I am going to design it in Blend 3, don't worry this will be a simple design.
  1. Open the Solution in Blend 3.
     
  2. Add few TextBlocks, TextBoxes.

    The MainPage.xaml will look like as follows:

    2.gif

    As you see from the above figure, I have 3 text boxes for User Name, Email ID, and Age. I have 2 Password Boxes for Password and confirm Password. All arefor User Input.

    Now design part is done open the solution in Visual Studio Again. Here is the Xaml Code after designing.

          
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
       
     
  3. Now we will add a class to the Silverlight Project and Name is UserInfo.cs

    3.gif
     
  4. We will implement INotifyPropertyChanged interface to view the notifications.
     
  5.  Add a method that can notify when there is a property change.

    private void RaisePropertyChanged(string propertyName) 
    { 
      if (this.PropertyChanged != null) 
      { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
    }
That's it now we are ready with our demo. Press F5 to run in Debug mode or you can try with Start without Debugging (Ctrl + F5).

You will see the error messages that you have provided in the properties in a red box which can be shown when you mouse hover onto it.

The following error messages will be thrown when there is an error with the user input.
 
 4.gif 
 
These are the error messages when you mouse over the red flag on the top right corner of the input.

The following figure displays when nothing is hovered.

5.gif
 
That's it. We have successfully imlemented input validation in Silverlight 3.

Enjoy Coding! 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend