May
30
Written by:
Steve Gray
5/30/2010 3:12 PM
Here is quick start example to code to demonstrate two principles in the Validation Application Block.
We’re going to show how validation works in a class, and on an ASP.NET page.
The concept is that you create a class that represents a data object (we’re using ‘customer’ here) and decorate the class with data validators. We’re only showing a ‘StringLengthValidator’, but if you get that working you’ll easily be able to look up the other types.
Download and install the Enterprise Library 5.0 objects
Create a web project, and set a reference to these dlls:
- Microsoft.Practices.EnterpriseLibrary.Validation.dll
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.ServiceLocation.dll
- Microsoft.Practices.Unity.dll
- Microsoft.Practices.Unity.Interception.dll
Add this code to your web site – you’ll need default.aspx and a class called ‘Customer’
First the ASP.NET page:
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet"
Namespace="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet"
TagPrefix="cc1" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="lblError" runat="server" ForeColor="Red"></asp:Label>
<table>
<tr>
<td>Customer Name</td>
<td>
<asp:TextBox ID="txtCustname" runat="server" MaxLength="65" Text="a"></asp:TextBox>
<cc1:propertyproxyvalidator
id="Propertyproxyvalidator1"
runat="server"
sourcetypename="Customer"
propertyname="custname"
controltovalidate="txtCustname"
ForeColor="Red"
EnableClientScript="true" ></cc1:propertyproxyvalidator>
</td>
</tr>
<tr>
<td>Customer Number</td>
<td>
<asp:TextBox ID="txtCustnmbr" runat="server" MaxLength="15" Text="b"></asp:TextBox>
<cc1:propertyproxyvalidator
id="CustnmbrValidator"
runat="server"
sourcetypename="Customer"
propertyname="custnmbr"
controltovalidate="txtCustnmbr"
ForeColor="Red"
OnValueConvert="CustnmbrValidator_ValueConvert"></cc1:propertyproxyvalidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</asp:Content>
Here’s the code behind:
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Integration
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.lblError.Text = ""
Me.lblError.ForeColor = Drawing.Color.Red
End Sub
Protected Sub CustnmbrValidator_ValueConvert(ByVal sender As Object, ByVal e As ValueConvertEventArgs) Handles CustnmbrValidator.ValueConvert
'this executes before the btnSubmit code, and only affects the 'validated value' for the
'textbox itself
'this value is not stored in the textbox, and the btnSubmit code doesn't see it
e.ConvertedValue = "cccc"
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
If IsValid Then
Dim oCust As New Customer
Dim strError As String = ""
'above, custnmbr was converted. But here, we only see the actual value in the text box.
oCust.custnmbr = Me.txtCustnmbr.Text
oCust.custname = Me.txtCustname.Text
Dim myValidator As Microsoft.Practices.EnterpriseLibrary.Validation.Validator = ValidationFactory.CreateValidator(Of Customer)()
Dim myResults As New ValidationResults()
myValidator.Validate(oCust, myResults)
If Not myResults.IsValid Then
strError = "Error:<br/>"
For Each vr As ValidationResult In myResults
strError += vr.Message & "<br/>"
Next
Throw New Exception(strError)
End If
Me.lblError.Text = "Successful Validation"
Me.lblError.ForeColor = System.Drawing.Color.ForestGreen
End If
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
End Class
And finally the class:
Imports Microsoft.VisualBasic
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators
Public Class Customer
<StringLengthValidator(3, 10, MessageTemplate:="The customer name must be between 3 and 10 characters")> _
Public Property custname As String
<StringLengthValidator(4, 10, MessageTemplate:="The customer number must be between 4 and 10 characters")> _
Public Property custnmbr As String
End Class
1 comment(s) so far...
Re: Enterprise Library Validation Application Block example
I found your website perfect for my needs. It is full of interesting and helpful posts. I have read most of them and explored a lot new stuff from them. You are doing some great work. Thank you for making such a nice website.
By .net chat on
8/17/2010 7:32 AM
|