Retaining Entered Data When Using Custom Validation

3 posts, 0 answers
  1. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 06 Sep 2010 Link to this post

    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. Martin
    Admin
    Martin avatar
    811 posts

    Posted 09 Sep 2010 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 09 Sep 2010 Link to this post

    /facepalm

    That's the ticket, thanks.

    Richard
Back to Top