This is a migrated thread and some comments may be shown as answers.

What is correct method for adding custom validator to GridBoundColumn in RadGrid

5 Answers 345 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 02 Oct 2008, 03:27 AM
I've been trying to add a custom validation control to a GridBoundColumn following the directions on page 439 Section 20.14.1 of the documentation.

It works fine for required validation controls, and for regularexpression controls, but I have not succeeded in getting it to work for custom validation controls.

What is the correct event handling wire up that should be used? In other words, what is the programmatic equivalent of the declarative "OnServerValidate"?

I also tried to add a custom validation control in the declarative markup on the aspx page but I couldn't get that to work either.

Is the only way to do it declaratively with a TemplateColumn instead of a GridBoundColumn? Or if it can be done programmatically in the codebehind file, then what is the correct way to add the server validate event handler?

Thanks much for your help.

5 Answers, 1 is accepted

Sort by
0
Carl
Top achievements
Rank 1
answered on 02 Oct 2008, 05:45 AM
I managed to get the custom validator working by using an AddHandler statement in the RadGrid1_ItemCreated routine, but now my problem is that I'm unable to get access to another field that I need for the validation in the custom validation event handler.

It would be nice to see the complete example for how to do a custom validation control for which the custom validation event handler needs to access data from another field of the same row, ie, a different field than the one being validated.
0
Sebastian
Telerik team
answered on 02 Oct 2008, 10:34 AM
Hi Carl,

How to access controls residing in a different cell of the same edited grid row you can see from the following online demo of the product:

http://demos.telerik.com/ASPNET/Prometheus/Grid/Examples/Programming/AccessingCellsAndRows/DefaultCS.aspx

as well as from this topic in the documentation:

http://www.telerik.com/help/aspnet-ajax/grdvalidation.html

Feel free to modify/extend the solution to meet your custom requirements. You may also be interested in learning more about the flexible server-side validation mechanism of RadGrid demonstrates in this example:

http://demos.telerik.com/ASPNET/Prometheus/Grid/Examples/DataEditing/Validation/DefaultCS.aspx

Best regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Carl
Top achievements
Rank 1
answered on 02 Oct 2008, 04:26 PM
Well thank you but I did look at the documentation you mentioned, but it just does not answer my questions.

First of all the ServerValidate routine needs to access a Guid value in the ListValueField of a GridDropDownColumn which is in another column of the same row for the column that is being validated in the ServerValidate routine.

There are several problems:

1) It is not at all clear to me which event to use in order to access the correct and current value of the Guid in the GridDropDownColumn. Most of the events fire AFTER the ServerValidate routine is triggered so those events canNOT be used.

2) The documentation describes a complicated sequence of getting the row, then getting the column via the UniqueName, then getting a string value. But then a string cannot be converted to a Guid so it cannot be used in the validation routine which requires the Guid to check for prior existence of a separate string in the field being validated together with the grid from the dropdowncolumn. This pair, the string from the field being validated together with the guid from the dropdowncolumn, are then submitted to the database via a LINQ toSQL stored procedure to check for prior existence of the combined index (string + guid).

In general, I find the whole approach to accessing the current selectedvalue of a dropdowncolumn unnecessarily complicated and confusing and very UNlike the simple approach used by standard ASP.NET. Please develop a simpler more convenient approach for this situation!

meanwhile, can you please answer more carefully my questions. Again the generic documentation has NOT helped in this situation.

Thank you
0
Carl
Top achievements
Rank 1
answered on 03 Oct 2008, 06:34 AM

Finally, I got something working. I was able to convert the guids from string to guid by using 

theResourceGuid =

New Guid(e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("ResourceGuid").ToString)

and I was also able to get the other guid I needed from the GridDropDownColumn by putting another CustomValidator on that column. It seems awkward and unnecessary because that column does not need validation! But it was the only way that I could get it to work.

So what's the recommended way of doing this more cleanly? Using a session variable? Or which event do I access that would be fired prior to the CustomValidator ServerValidate events that all fire before the ItemCommand event?

0
Sebastian
Telerik team
answered on 06 Oct 2008, 08:45 AM
Hi Carl,

I apologize if my directions were not helpful enough for your particular case - I agree that they are relevant for common cases and may not be applicable for your very situation.

It seems that you discovered one applicable approach when having GUIDs to compare. Generally speaking, to compare both GUID values you may also reference the SelectedItem.Value property value of the GridDropDownColumn editor to compare them directly (since it will hold the GUID value which corresponds to the selected item in the dropdown editor).

There might be different approaches for attaining the validation mechanism you are searching for which depend on your custom logic. If you would like to modify your implementation or utilize the page/control lifecycle, you may find the following help article useful.

Kind regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Carl
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or