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

Retaining Entered Data When Using Custom Validation

2 Answers 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard Weeks
Top achievements
Rank 2
Richard Weeks asked on 07 Sep 2010, 12:47 AM

I have a RadGrid that takes data from a business object of some sort. I use the Microsoft Enterprise Library 4.1 to conduct validation on the object by decorating with a [HasValidation] attribute and handling accordingly.

I'm having problems working out how to just pass back validation errors without ending the insert / update process. I'll elaborate.

For:

OnUpdateCommand="TelerikGrid_OnUpdateCommand"

I have:

protected void TelerikGrid_OnUpdateCommand(object source, GridCommandEventArgs e)
{
    this.businessObject = new BusinessObject();
  
    this.businessObject.GetGridValues(e);
  
    var validationResults = Validation.Validate(this.businessObject);
  
    if (validationResults.IsValid)
    {
        this.businessObjectData = new BusinessObjectData();
  
        int updateResult = this.businessObjectData.Update(this.businessObject);
  
        switch (updateResult)
        {
            case -1:
  
                this.Master.FeedbackText = "Not updated.";
  
                break;
  
            default:
  
                this.Master.FeedbackText = "Updated.";
  
                break;
        }
    }
    else
    {
        this.Master.FeedbackText = string.Format(
            "Errors found in submission: {0}",
            ValidationHelper.GetErrors(validationResults, false));
    }
}

Everything works very well but if there are validation errors, the errors are displayed but the grid appears to rebind, collapsing the edit form and losing all the entered values in the process.

How can I retain the values entered and stop the edit form collapsing when the data fails validation?

Regards,
Richard

2 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 09 Sep 2010, 12:02 PM
Hello Richard,

I would suggest that you set e.Canceled="true" inside your UpdateCommand event handler when the validation fails:

protected void TelerikGrid_OnUpdateCommand(object source, GridCommandEventArgs e)
{
    ...
    if (validationResults.IsValid)
    {
        ...
    }
    else
    {
            this.Master.FeedbackText = string.Format(
            "Errors found in submission: {0}",
            ValidationHelper.GetErrors(validationResults, false));
            e.Canceled = true;
    }
}

I hope this helps.

Sincerely yours,
Martin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Richard Weeks
Top achievements
Rank 2
answered on 10 Sep 2010, 12:19 AM
/facepalm

That's the ticket, thanks.

Richard
Tags
Grid
Asked by
Richard Weeks
Top achievements
Rank 2
Answers by
Martin
Telerik team
Richard Weeks
Top achievements
Rank 2
Share this question
or