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.
To make the application look good I am going to design it in Blend 3, don't worry this will be a simple design.
- Open the Solution in Blend 3.
- Add few TextBlocks, TextBoxes.
The MainPage.xaml will look like as follows:
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.
- Now we will add a class to the Silverlight Project and Name is UserInfo.cs
- We will implement INotifyPropertyChanged interface to view the notifications.
- 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.
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.
That's it. We have successfully imlemented input validation in Silverlight 3.