ASP.NET Dynamic Data and Validation Using LINQ To SQL OnPropertyChanging Partial Methods
by David Hayden, Florida ASP.NET Developer, Filed: ASP.NET 3.5
ASP.NET Dynamic Data is one of the new technologies showcased in the ASP.NET 3.5 Extensions CTP released a few weeks ago. It is all about building database-driven web applications quicker and with less effort. If you haven't played with it yet, you can get up to speed by viewing the past two tutorials:
Quick Review - Validation Using Attributes on LINQ To SQL Classes
In the previous tutorial I talked about how Dynamic Data comes with some Validation Attributes that you can use to decorate your partial LINQ To SQL Classes to help with input validation. These attributes are part of the System.Web.DynamicData Namespace in System.Web.Extensions.dll and some examples are:
- RangeAttribute
- RequiredAttribute
- RegexAttribute
By adding a RangeAttribute on the Partial Product LINQ To SQL Class as such:
[Range("UnitsInStock",10,50,
ErrorMessage="Must be between {1} and {2}")]
public partial class Product
{
}
I can disallow any values of UnitsInStock that are not between 10 and 50:

Very cool. The use of attributes for validation is pretty common. You can find this in the Validation Application Block, Castle.Component.Validator, etc.
Another Validation Option - Using LINQ To SQL OnPropertyChanging Partial Methods
In the current release of Dynamic Data, LINQ To SQL is used as the database provider. LINQ To SQL offers a number of partial methods that allow you to validate the values being set to properties. Inside these methods you can do your own validation as opposed to using the attributes. For example, I can do the same validation mentioned above by adding the following code inside the OnUnitsInStockChanging Partial Method offered via LINQ To SQL:
public partial class Product
{
partial void OnUnitsInStockChanging(short? value)
{
if (value < 10 || value > 50)
throw new
ValidationException("must be between 10 and 50");
}
}
Where ValidationException is just a custom exception I created for the example. You could have easily just thrown Exception, but it is not a good practice in production.
Now normally when this happens we just get an exception and have to deal with it manually. However, the DynamicValidator Control that is associated with the UnitsInStock TextBox in the DynamicGridView turns this exception into an error and display it accordingly. Here is the DynamicValidator Control,
<asp:DynamicValidator
runat="server"
ID="dynamicValidator"
ControlToValidate="TextBox1"
Display="Dynamic" />
and it is located in the App_Shared -> DynamicDataFields -> Text_Edit UserControl. The end results on the UI are the same:

Nice. If you reflect on the DynamicGridView's OnRowUpdated Event:
protected override void
OnRowUpdated(GridViewUpdatedEventArgs e)
{
base.OnRowUpdated(e);
if (((e.Exception != null) && !e.ExceptionHandled)
&& !this.PageIsValid())
{
e.ExceptionHandled = true;
e.KeepInEditMode = true;
}
}
You will notice that it is swallowing the exception so that the message can be appropriately displayed as an error on the page. Essentially we have moved the validation of data fields from the presentation layer into the data layer where it can be implemented in all cases - not just from the UI.
Conclusion
In essence, ASP.NET Dynamic Data is giving you the option of doing input validation using 1) Attributes or 2) a DynamicValidator Control that works with thrown exceptions within the Partial Methods in the LINQ To SQL Classes. This is all built-in for you to help create database-driven ASP.NET applications in a more efficient and productive manner.
Site: http://www.davidhayden.com
Author: David Hayden
Tag: ASP.NET 3.5