Using PropertyProxyValidator in code

Topics: Validation Application Block
Mar 30, 2007 at 3:41 PM
Hi,

I am evaluating whether it is possible to use the VAB using code only - i.e. in a designerless environment. We have a application framework where we use configuration files to generate controls. Now we want to enhance this so that PropertyProxyValidator objects are created alongwith controls using custom configuration files.

Is this possible? Would I have to create the PropertyProxyValidator objects in a specific event and assign properties?

Regards,
Neel
Mar 30, 2007 at 3:52 PM
Hi Neel -

Even when you are using the Winforms designer, ultimately everything comes down to code at the end - so what you're describing should indeed be possible. I'd suggest building a form that has the required behavior, and then looking at the generated code in the form.designer.cs/vb file for inspriation.

HTH
Tom
Mar 31, 2007 at 6:50 PM
Hi Tom,

Yes, I tried that. I am a newb to EL, so apollogies if my questions sound stupid. Here's what I did:
1. Created a Web project with two pages: DesignerValidations.aspx and CodeValidations.aspx. The idea was to get a working PoC using VS (2005) designer and then try to code it.
2. Added a DLL project to the solution.
3. Created Config file for the DLL project.
4. Added a Type called ExpenseReport with single property Email. No validations defined as attribs.
5. Added Validation section using VS config editor. Added the ExpenseReport Type and a single Email property. Added a pattern validation in the Config file for Email property.
6. DesignerValidations works just fine. CodeValidations.aspx doesn't do anything, just happily posts back. Also, I am not explicitly calling Validate method in the designer based page, so is entlib doing that for me somehow? How then do I emulate this in code?

Regards,
Neel
Mar 31, 2007 at 6:52 PM
Sorry - one more thing to add - the .cs file for DesignerValidations.aspx is empty. No code fr me to look at.
Mar 31, 2007 at 10:59 PM
Oh sorry, I was thinking you were using the winforms integration, not ASP.NET - so my advice is completely wrong :-S

I'm not sure how you could do this in an ASP.NET application, but I'm not very wise in the ways of ASP.NET. Hopefully someone else has some suggestions?

Tom
Apr 30, 2007 at 10:10 AM
Anyone has any more views on this? Looks like time to dig into Entlib code?!

Regards,
Neel
Apr 30, 2007 at 2:55 PM
Neel,

You can use the PropertyProxyValidator control programmatically like any other control. Here is some code I whipped up really quickly to add a control and have it validate a txtName Control on a web page:

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
            }
        }
    }
}

The Customer Class looks like:

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

Regards,

Dave

_____________________________________

David Hayden
Microsoft MVP C#