ASP.NET Dynamic Validator

misc_vol3_100

One of the controls that was added to ASP.NET 3.5 in the SP1 release was the Dynamic Validator control.

I completely missed it.

What it does is pretty cool.  But it doesn’t really do what you’d think it might.  Or at least not what I thought it would.  “Dynamic” implies to me some kind of hook up to the database.  But the Dynamic Validator control doesn’t hook to the database.  At least not directly.  What it does, however, is a lot more flexible.

You would use the Dynamic Validator control in a lot of the same instances that you’d use the Custom Validator.  While the Custom Validator has a lot more flexibility, the Dynamic Validator is a bit easier to implement.

The Dynamic Validator displays any exceptions that the OnChanging event of a field in your datasource.  This allows you to put all of your validations in the OnChanging event of the control rather than an event handler of the Custom Validator.

To demonstrate, place a TextBox control and a Dynamic Validator control on your page and set the ControlToValidate property to point to the TextBox control.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<cc1:dynamicvalidator runat="server" ID="dynamicValidator"
   ControlToValidate="TextBox1"></cc1:dynamicvalidator>

You’ll need to set up some sort of databinding between the text box and a datasource because the validation happens at the field level, not at the display level.

To implement the validations, create an OnFieldChanging event handler of a partial class that represents the row you’ve bound for the field the TextBox is bound to and create some sort of check.  Throw an exception if the check fails.

The validation control will then pick up the exception and display it for you.

Other places talking about the DynamicValidator control

Passing Arguments to a Dynamic Data Field Template from a UIHint … -  The ASCX is pretty basic. All that is here is my UI control and the needed validation controls. … Generic Access to ASP.NET Dynamic Data UIHint Attribute Values. Friday, May 09, 2008 1:01 PM by Craig Shoemaker. Yesterday I published a post titled, Passing Arguments to a Dynamic Data Field Template from a UIHint &#8230;

custom validation and business logic in asp.net dynamic data using … – the exception that is raised by this code will be caught and displayed to the user by the dynamicvalidator control located in the corresponding dynamic field template. the field template in this case is the integer_edit.ascx file, …

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear