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

RadGrid with ServerValidate - obtaining DataKeyValue - problem when add new row

2 Answers 40 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Darrell
Top achievements
Rank 1
Darrell asked on 24 Oct 2014, 04:38 AM
Hi, I'm using the following code to get the DataKeyValue of the edited row in the ServerValidate function. This is working well when editing a record. However, when creating a new record, item.GetDataKeyValue("ID") throws an argument out of range exception. Is there a way that I can test whether a DataKeyValue exists before making this call? Thanks.
protected void URLGrid_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
   {
       CustomValidator val = (CustomValidator)source;
       GridDataItem item = (GridDataItem)val.NamingContainer;
           intwebsiteID = int.Parse(item.GetDataKeyValue("ID").ToString());
       if(!Website.URLIsUnique(websiteID, args.Value.Trim()))
       {
           args.IsValid = false;
       }
   }




2 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 28 Oct 2014, 04:14 PM
Hello Darrell,

The easiest way for handling this would be to retrieve the DataKeyValue only when the GridDataItem is not GridDataInsertItem (or GridEditFormInsertItem if you are using EditForms as edit mode):
CustomValidator val = (CustomValidator)source;
GridDataItem item = (GridDataItem)val.NamingContainer;
if (!(item is GridDataInsertItem))
{
    intwebsiteID = int.Parse(item.GetDataKeyValue("ID").ToString());
    if (!Website.URLIsUnique(websiteID, args.Value.Trim()))
    {
        args.IsValid = false;
    }
}

You could also wrap the line where you are trying to retrieve the DataKeyValue within a Try/Catch.

Hope this helps.
 

Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Darrell
Top achievements
Rank 1
answered on 28 Oct 2014, 09:48 PM
Thanks, Konstantin - if (!(item is GridDataInsertItem)) did the trick.
I prefer avoiding try catch where possible
Cheers
Darrell

Tags
Grid
Asked by
Darrell
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Darrell
Top achievements
Rank 1
Share this question
or