The Enterprise Library is designed to help developers with common tasks just as validation, logging, exception handling, data access, …)
In this post I will show you how to validate the content in a WebPart by making use of the Enterprise Library.
There are 2 ways of creating validation. One is to set your validation rules in the class itself. The other way is to make use of a .config file.
First of all create a new WebPart project.
In the first part we will make use of the validation in the class itself.
You can create a class (here I make use of the class Person)
My Person class has an property “Name”.
If we want to make a validation for the Name that sets the max size to 10 characters we can use following code:
(Before you can use the Enterprise Library you need to install it: http://www.microsoft.com/downloads/details.aspx?FamilyId=1643758B-2986-47F7-B529-3E41584B6CE5&displaylang=en )
You need to set a reference to Microsoft.Practices.EnterpriseLibrary.Validation
In an onclick event of a button you can set the code to create and validate the object.
If you want to make use of the Enterprise Library Configuration Tool you need to have a .config file in your project.
(I make use of a .config file called Validation.config and placed in the WebPart folder)
In the Enterprise Library Configuration Tool you can open an application by selecting the .config file that you just created.
In this tool it is easy to set some validation to the Person class.
If you now want to make use of this .config file you need to explicitly say that when validating the object. This can be done with following code:
Here some screenshots of my WebPart with the validation.
PS: don’t look at the layout, this is an example 🙂
The Enterpise Library is a cool tool because you can edit your validation rules without going in the code and you can change validation rules in no time.
I hope this can help you to implement the enterprise library into your webparts where user-input is needed.