IDataErrorInfo for ASP.NET

As far as I'm aware ASP.NET doesn't support IDataErrorInfo, I've asked about this in many places, including Tech.Ed '07 with no success. The closest things I've seen in terms of Business Object level validation is from Enterprise Library validation block (which is attribute based and can render out with custom EntLib web controls) and another example in the Futures Dynamic Data Controls using Linq (I have no idea how this magic validation magically appears).

It all just seems overly complicated, so, have a look at the IDataErrorInfo interface the methods are:

string this [ string columnName ] { get; }
string Error { get; }

Simple and generic. I've been playing around with an idea about how to get this into a generic web control, introducing:

WebValidationControlExtender

Here is an example of the control in design mode:

image

Here is an example of the control in action using an ObjectDataSource and a plain old DetailsView:

image

Here's what's going on under the hood.

All you need to do is tell WebValidationControlExtender what ObjectDataSource to hook in to.
When the page is executing and the user tries to insert a record, the DetailsView control builds up an object and sends it into the ObjectDataSource.
WebValidationControlExtender is already listening for Insert and Update events, all it does is check if the object being passed through implements IDataErrorInfo.
If it does, WebValidationControlExtender interrogates the object's columns/properties for errors.
If errors are found, it adds CustomValidators back to the page then goes searching for controls that are using that object datasource (in this case the DetailsView).
When it finds the details view it searches for BoundFields that are bound to the properties with the error, then inserts the red * like in the picture above.

The point is, you could go an implement whatever validation library suites your needs, as long as the object implements IDataErrorInfo the validation works. I just don't understand why ASP.NET doesn't already use something like this, there are so many complicated implementations for validation, I hope this brings some fresh light.

.NET ASP.NET Code Snippets
Posted by: Brendan Kowitz
Last revised: 21 Sep 2013 12:15PM

Comments

Jan
Jan
1/16/2008 1:36:30 PM
I tried to modify your code to support the LLBLGenDataSource. It seems to work fine, but I cannot gent the * to display in case of an error. The validation summary is show.

When I debug the code I see that a CustomValidator being added to the page. However, the page does not show it.

Any suggestions?
1/18/2008 10:54:07 AM
Hi Jan,

There are a few restrictions with the current code.
The '*' are currently only supported for DetailsView, I have intentions for supporting GridView but haven't found the time. The only other restriction is the '*'s are only inserted on BoundColumns.
If you have ideas or features you'd like to see supported, I'd like to hear them, or even submit changes/fixes.

Brendan.
3/21/2009 6:47:57 AM
The link to code is not working
3/21/2009 11:19:33 AM
Sorry, link is now updated. This class has been moved in the repo, I should have included a revision number in the link so the url was always accessible but I wanted updates that are made to be visible.
Incase this ever happens again here’s a permalink for what’s there now:
code.google.com/.../WebValidationControl.cs

No new comments are allowed on this post.