Validation Application Block - Programmatically Creating PropertyProxyValidator on ASP.NET Web Page

Validation Application Block - Programmatically Creating PropertyProxyValidator on ASP.NET Web Page

by David Hayden ( Florida ASP.NET Developer ), Filed: Enterprise Library 3.0

 

A question came up in the Enterprise Library 3.0 Forums on how to create and add a PropertyProxyValidator Control to your web forms programmatically. If you are unfamiliar with the Validation Application Block and the PropertyProxyValidator Control used to integrate ASP.NET with the Validation Application  Block, see a couple of posts of mine:

Creating and adding a PropertyProxyValidator Control to your web forms programmatically is just like adding any other control to your web form programmatically and here is some sample code that adds one to validate the txtName TextBox Control on the form against the rules on the Name Property of the Customer Class ( that's a mouthful ):

 

namespace PropertyProxyValidatorViaCode
{
    public partial class _Default : Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            PropertyProxyValidator control =
new PropertyProxyValidator(); control.PropertyName = "Name"; control.ControlToValidate = "txtName"; control.SourceTypeName =
"PropertyProxyValidatorViaCode.Customer"; form1.Controls.Add(control); btnAdd.Click += new EventHandler(btnAdd_Click); } void btnAdd_Click(object sender, EventArgs e) { if (Page.IsValid) { // Do Something } } } }

 

Here is the Customer Class:

 

namespace PropertyProxyValidatorViaCode
{
    public class Customer
    {
        private string _name;

        [StringLengthValidator(1,10)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
}

 

Hope this helps.

Source: David Hayden ( Florida ASP.NET Developer )

Filed: Enterprise Library 3.0

 

posted on Monday, April 30, 2007 10:04 PM

Main

David Hayden Google +

David Hayden Twitter

Health & Fitness

JavaScript Patterns Book Review

HTML 5 and CSS3 - Develop with Tomorrow's Standards Today

Professional ASP.NET Design Patterns Book Review

Beginning Mac Programming Book Review

C# in Depth Book Review

ASP.NET MVC

Orchard CMS

Categories