.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

Data Annotations In Silverlight Application

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

In this article we will see how Data Annotations help us in validating the data in Silverlight Application with an example.
 

Introduction

In my last article titled "Data Validation In Silverlight 3 Application", we discussed about how we can validate the user input. In this article we will see how Data Annotations help us in validating the data in Silverlight 3 Application.

Crating Silverlight Project

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

image1.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:

    image2.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

    image3.gif
    In my previous article on Data Validation, I had implemented INotifyPropertyChanged interface. Here we don't need that. We will see how it is not required.
     
  4. Add a Reference to System.ComponentModel.DataAnnotations

    image4.gif
     
  5. Now we will add properties to our UserInfo.cs class.
    As we are following the same user inputs, I am adding the same properites. Follow the code below:
    public class UserInfo
    {
        #region UserName
        private string _UserName;
        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }
        #endregion
        #region EmailID
        private string _EmailID;
        public string EmailID
        {
            get { return _EmailID; }
            set { _EmailID = value; }
        }
        #endregion
        #region Age
        private string _Age;
        public string Age
        {
            get { return _Age; }
            set { _Age = value; }
        }
        #endregion
    }

  6. Now Add required attributes to the properties, for example Required, Range, and RegularExpression etc..

    See the code below how User Name property is being validated. Add the code before the property.
    [Required(ErrorMessage="User Name is Required")]
    [StringLength(12,MinimumLength=6,ErrorMessage="User Name must be in between 6 to 12 Characters")]

  7. Add ValidatorProperty method with required parameters.
    [Required(ErrorMessage = "User Name is Required")]
    [StringLength(12, MinimumLength = 6, ErrorMessage = "User Name must be in between 6 to 12 Characters")]
    public string UserName
    {
        get { return _UserName; }
        set
        {
            Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "UserName" });
            _UserName = value;
        }
    }

  8. For Email ID validation we need Regular Expression, go ahead write the following code.
     
  9. For Age validation we need Range, add minimum and maximum values.
     
    [Range(18, 40, ErrorMessage = "Age must be 18 ~ 40")]
    public string Age
    {
        get { return _Age; }
        set
        {
            Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age" });
            _Age = value;
        }
    }

  10. Last but not the least add the UserInfo instance to the LayoutRoot's DataContext.

    Remebmer you can add it in two ways, in C# code behind as well as in Xaml. In my last article I added like the following:

    serInfo user = new UserInfo();
    this.LayoutRoot.DataContext = user;

    Now you can do the same thing in XAML too, add the namespace referrence and then do as follows:

    xmlns:myData="clr-namespace:DataAnnotationsInSL3"



    Now Run your application. Enter wrong input and you will get the error messages.

    image5.gif
That's it we have successfully used Data Annotations to validate user input, and without using INotifyPropertyChanged interface.

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