Saturday, December 15, 2012

…validations…validations…

It was a day of thunder in September. Was raining heavily outside and I was sitting on my desktop, coding and sipping coffees over coffees. There was an issue where we were stuck! We need to perform validations in silverlight. Well silverlight did have this so called powerful validation algorithm but the situation was little over complicated.  Client side validations was needed to be applied. Something was needed to be done and that too very soon.
After being frustrated of nowhere taking hit and trials. I decided to write some sample code to achieve the goal. The two Interfaces that are only known to be helpful in validating.
  • INotifyPropertyChanged
  • IDataErrorInfo
I’m gonna tell you exactly how I reached their, yes the validations of course.
1. Let’s first open a silverlight applications. Named it as SilverSurfer (one of my favorite movie).
2. Next we place a button on the exact center of the screen in the main page.
3. After that we add a Window in our solution. Right click on the client project, go to Add -> New Item -> Silverlight child window and name that window as Surfer.
4. Finally our surfer window is ready with our SilverSurfer. Let us now place a Text box in the Surfer window. Well yes! it is important.
Adding text box is very simple, you just need to write a line of xaml for that. The whole xaml of child window surfer, is shown below.
5. Now time to call Surfer from SilverSurfer. In C#, you need to create an object of Surfer, SurfBoard, let’s say. Then call a function “SurfBoard.Show()” to open the window. So, in the click event of Button of the SilverSurfer Screen write the following code.

6.  Next we need to write hell lot of code. First of all the Surfer class, which already had inherited ChildWindow Class, now implements two interfaces that were previously mentioned. INotifyPropertyChanged and IDataErrorInfo. Here’s the code my Surfer Class possesses.
 public partial class Surfer : ChildWindow, INotifyPropertyChanged, IDataErrorInfo  
 {  
 bool isokclicked = false;  
 //property that is binned in XAML with the textbox.  
 string _TopicP;  
 public string TopicP  
 {  
 get  
 {  
 return txtName.Text;  
 }  
 set  
 {  
 this._TopicP = value;  
 txtName.Text = value;  
 OnPropertyChange(“TopicP”);  
 }  
 }  
 public Surfer()  
 {  
 InitializeComponent();  
 }  
 private void OKButton_Click(object sender, RoutedEventArgs e)  
 {  
 Error = null;  
 isokclicked = true;  
 OnPropertyChange(“TopicP”);  
 if (String.IsNullOrEmpty(Error))  
 {  
 this.DialogResult = true;  
 }  
 }  
 private void CancelButton_Click(object sender, RoutedEventArgs e)  
 {  
 this.DialogResult = false;  
 }  
 string _Error;  
 public string Error  
 {  
 get  
 { return this._Error; }  
 set { this._Error = value; }  
 }  
 public string this[string Property]  
 {  
 get  
 {  
 switch (Property)  
 {  
 case “TopicP”:  
 {  
 //Condition to check for the error. Here the text box do not suppose to be empty. In case it is empty, the error will be displayed.  
 if (String.IsNullOrEmpty(this.TopicP) && isokclicked)  
 {  
 Error = “Data Error”;  
 return “Please enter the topic.”; //Error that is returned  
 }  
 } break;  
 }  
 return null;  
 }  
 }  
 public event PropertyChangedEventHandler PropertyChanged;  
 protected void OnPropertyChange(string propertyName)  
 {  
 if (this.PropertyChanged != null)  
 {  
 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
 }  
 }  
 private void ChildWindow_Loaded(object sender, RoutedEventArgs e)  
 {  
 LayoutRoot.DataContext = this;  
 }  
 }  

Well simple as it seems. This is how I apply client side validations in Silverlight.
Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top