Infragistics Article

WebDataGrid Validation

Quick Summary:

  • Associate validators and editor providers to WebDataGrid cells using Cell Editing’s Column Settings
  • Customize the look and feel of validation messages by applying a CSS class to the validator
  • Wire up custom validators by finding editor provider IDs in the generated HTML

Introduction

One of the strengths of the WebDataGrid is giving the user the ability to edit data in the grid directly in the table cell. This article will take you through the steps required to associate a validators to the grid. The validation controls used in this sample will demonstrate how to use stock controls like the RequiredFieldValidator as well as a CustomValidator.

The WebDataGrid wires up validation with the definition of a column setting. Column settings associate an editor provider and a validation control to a column. When these two components are linked together in a column then validation is enabled.

Be sure to watch the WebDataGrid Validation video and download the sample code.

Adding Editor Providers

The first step to providing validation to the grid is to place some editor providers on in the grid. Once you have a ScriptManager and the WebDataGrid on the page, open the smart tag in the designer and select Edit Grid Editor Providers.

When the Editor Provider dialog opens you’ll have an opportunity to define an editor provider. When you click the “+” button you can select from a list of editors. For the sample implemented in this article, the first name is being validated as being required and the LastLoginDate is must be a date earlier than today. Therefore a TextEditorProvider is added for the first name field and the DateTimeEditorProvider is included for the LastLoginDate field.

Validators

Next you must add the validators to the page. The following listing demonstrates how to customize the markup for a required field validator as well as a custom validator.

These validation controls will behave slightly differently than what you may be used to under other circumstances. Looking first at the code for the RequiredFieldValidator, notice that the ControlToValidate property is set equal to the ID for the WebDataGrid. Usually you would set the ControlToValidate to the ID of a concrete input control. The WebDataGrid will intercept the call for the validator to attach to an input control and will route the validation messages from the selected cell’s input control to the validation control.

Another difference is that you may be used to adding a marker like an asterisk in the Text property of the validator choosing to allow the ValidationSummary control to display the ErrorMessage value to the user. Since the validation messages are displayed next to the input controls a ValidationSummary control is unnecessary and the value of the Text property is the member used to display information to the user.

The last anomaly (which is completely un-related to the use of the WebDataGrid) is the need to blank out the validator’s ForeColor property. The ASP.NET validation controls have a hard-coded value for their ForeColor property which is an unsightly bright red. Having an explicit value in this property will force the control to generate an in-line style thereby overwriting any styles applied higher up in the CSS hierarchy. Blanking this value out will allow the error class to appropriately style the control.

Behaviors

Now that the page has validators and the grid has editor providers defined, the next step is to add the editing behaviors to orchestrate the CRUD operations and handle the client-side validation.

Return to the smart tag and open the Behaviors Editor.

You must enable and customize a number of behaviors on the grid:

  1. Activation: The activation behavior is required in order to fire the client events required for the validation listeners
  2. Editing Core: The core is required when you use any of the editing behaviors
  3. Cell Editing: Cell editing is the behavior that will allow you to customize the ColumnSettings to associate the editors with the validators (see below)
  4. EnableOnKeyPress: Enabling this mode action will allow the control to switch to edit mode when the user starts typing (not required, but nice)

Next click on the (Collection) next to ColumnSettings, which opens the ColumnSettings editor:

A column setting definition is required for any column that needs validation enforced. When you click on the Add Item button you can associate the editor provider to a validator by the column key. Making these associations will properly define the validators needed for each column.

Custom Validator

Working with a custom validator is possible, but requires some extra steps which are detailed below. The CustomValidator control requires that you provide the function name defined on the client that encapsulates the custom validation logic for the control. The following listing is the script used in this instance:

This validation function locates the instance of the WebDateTimeEditor on the page, extracts it’s value and compares it with the current date. What you must be aware of about this code is that the only way to get the value for the ID of “wdg_ct101” is to view source on the page and find what ID is being generated for that control.

When you view source on the page and scroll to the bottom of the page, you will find a script block that defines the Infragistics controls on the page. Since the validation script on this page is concerned with the WebDateTimeEditor’s value you simply need to look for the definition of the WebDateTimeEdior and find it’s ID value.

Note: This approach is a very brittle method to use for finding and accessing controls on the page. If you add new editor providers, or perhaps any other controls to the page, there is a chance that the ID could change. If you choose to use this method make sure you test your code as the project evolves!

Server Validation

A question people often ask is whether or not the WebDataGrid validators are run on the client as well as the server. In the grid’s current state the validation rules are only run on the client.

Resources

Recent Comments

By: Autumn Posted on 12-28-2011 5:02 PM

Should the same work with the WebHierarchicalDataGrid?  My error is "Message: Control 'whdgAddress' referenced by the ControlToValidate property of 'valEmailAddress' cannot be validated.", where whdgAddress is my grid and valEmailAddress is my validator.  The field I am trying to edit is in the child band of the grid.  

By: mflach Posted on 10-17-2011 3:43 PM

It looks like this dones't work with WebHierarchicalDataGrid.  When I try and set the WebHierarchicalDataGrid as the ControlToValidate get an error at runtime:

Control 'ProjectsGrid' referenced by the ControlToValidate property of 'ProjGridValidator' cannot be validated.

Thoughts?

By: infrauser1gic Posted on 02-25-2011 4:46 AM

Hi, It's been a while since this post but after executing the sample scripts, the var value = new Date($find("wdg_ctl01").get_date()); returns this value: "Feb 1 00:00:00 UTC+0800 2010". It is not getting the actual input date thus always valid.

By: majensen Posted on 10-08-2010 10:30 AM

After adding EditorProviders to allow inline cell editing I am getting an error regarding duplicate control id's when doing an async postback.  The WebDataGrid in question in housed in an UltraWebTab and if I navigate to another tab which triggers an async postback to load that tab's content and come back to the tab with the EditorProviders I get an error regarding duplicate control id's with "header1" or if I make some changes on a couple of rows and click a save button which triggers an async postback to persist the changes to the database when the grid is being refreshed the same error occurs.  The page/WebDataGrid worked fine before I added the EditorProviders.  Rather than being a help the Infragistics NetAdvantage suite is starting to become more of a hindrance in trying to get day to day development task accomplished because I have to take the time to troubleshoot spurious errors like this.  I am using the 2010.2.2040 build of the suite.

By: iqbal0304 Posted on 09-06-2010 2:09 AM

how to add multiple validator to a cell? In gridview we can add ones by specifying ControlToValidate property to the same control.

Sign in to post a comment to this article.