Tip: perform data verification in the Silverlight Application

Source: Internet
Author: User
Overview

As we all know, in ASP. in the. NET application, we can use the verification control to verify data input. Unfortunately, no verification control is provided in Silverlight, however, Silverlight provides some basic data verification support for bidirectional data binding. We can define verification rules in the set configurator and throw an exception for invalid data, finally, data verification is implemented by capturing verification error events.

This article describes how to perform data verification in a Silverlight application.

Prerequisites

In Silverlight, verification errors are triggered in the following two cases:

1. An exception is thrown during data conversion in the binding engine.

2. An exception is thrown in the set configurator of the business entity.

To receive notifications when a verification error occurs, we must set the following two attributes on the bound object to true:

ValidatesOnExceptions: tells the binding engine to create a verification exception when an exception occurs.

NotifyOnValidationError: tells the binding engine to trigger the BindingValidationError event when a verification error occurs or an error is eliminated.

These two attributes are defined in the Binding class, as shown in the following code:

The BindingValidationError event is defined in FrameworkElement and can receive parameters of the ValidationErrorEventArgs type. In ValidationErrorEventArgs, an important attribute Action is defined as follows:

Here, "Added" indicates a new verification exception, and "Removed" indicates that a verification exception is excluded. The following example shows how to use them for data verification.

Instance

First, we compile a simple business class. Because data binding verification can only be performed in two-way binding, we need to implement the INotifyPropertyChanged interface here, as shown in the following code, in the set configurator, we check the validity of the data. If the data is invalid, an exception is thrown:

/// <Summary> /// Author: TerryLee // http://www.cnblogs.com/Terrylee/// </summary> public class Person: INotifyPropertyChanged {public event PropertyChangedEventHandler PropertyChanged; private int _ age; public int Age {get {return _ age;} set {if (value <0) throw new Exception ("the Age input is invalid! "); _ Age = value; if (PropertyChanged! = Null) {PropertyChanged (this, new PropertyChangedEventArgs ("Age") ;}} private String _ name = "Terry"; public String Name {get {return _ name ;} set {if (value. length <4) throw new Exception ("the name input is invalid! "); _ Name = value; if (PropertyChanged! = Null) {PropertyChanged (this, new PropertyChangedEventArgs ("Name") ;}} public void policypropertychanged (String propertyName) {if (PropertyChanged! = Null) {PropertyChanged (this, new PropertyChangedEventArgs (propertyName ));}}}

Write Data Binding. The following Code sets the NotifyOnValidationError and ValidatesOnExceptions attributes to true and defines the BindingValidationError event:

<!--http://www.cnblogs.com/Terrylee--><StackPanel Orientation="Horizontal" Margin="10">    <TextBox x:Name="txtName"  Width="200" Height="30"            Text="{Binding Name,Mode=TwoWay,            NotifyOnValidationError=true,            ValidatesOnExceptions=true}"            BindingValidationError="txtName_BindingValidationError">    </TextBox>    <my:Message x:Name="messageName"></my:Message></StackPanel><StackPanel Orientation="Horizontal" Margin="10">    <TextBox x:Name="txtAge" Width="200" Height="30"            Text="{Binding Age,Mode=TwoWay,            NotifyOnValidationError=true,            ValidatesOnExceptions=true}"            BindingValidationError="txtAge_BindingValidationError">    </TextBox>    <my:Message x:Name="messageAge"></my:Message></StackPanel>

Implement the BindingValidationError event. Here, you can determine how to handle the event based on ValidationErrorEventAction, and provide related prompts on the interface, as shown in the following code:

/// <Summary> /// Author: TerryLee // http://www.cnblogs.com/Terrylee/// </summary> void txtAge_BindingValidationError (object sender, ValidationErrorEventArgs e) {if (e. action = ValidationErrorEventAction. added) {messageAge. text = e. error. exception. message; messageAge. validation = false;} else if (e. action = ValidationErrorEventAction. removed) {messageAge. text = "age verification successful"; messageAge. validation = true ;}}

Now let's take a look at the final verification results, as shown in:

In this way, we can verify the data input in Silverlight.

Summary

This article describes how to verify data in a Silverlight application. For more information about Silverlight 2, see Silverlight 2.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.