Search code examples
c#web-parts

How to set an error message from EditorPart when ApplyChanges returns false?


I'm developing a custom ASP.Net WebPart using the WebPartManager and I'm creating a custom EditorPart too. For its EditorPart.ApplyChanges method I set the return value to false whenever there is an error.

In the EditorZone I get a standard error message indicating that some error happened to the editor, but I want to change that message. Is that possible? Something like...

 public override bool ApplyChanges()
 {
  try
  {
     // save properties
     return true;
  }
  catch(Exception ex)
  {
     ErrorMessage = ex.Message; // is there any similar property I can fill?
     return false;
  }
 }

Solution

  • I've found one solution in social msdn, but I'm not sure it is correct because it is not very well documented. You have to set the error in the PreRender method, something like this:

    string _errorMessage;
    
    public override bool ApplyChanges()
    {
     try
     {
        // save properties
        return true;
     }
     catch(Exception ex)
     {
        _errorMessage = ex.Message; // is there any similar property I can fill?
        return false;
     }
    }
    
    protected override OnPreRender(EventArgs e)
    {
      if (!string.IsNullOrEmpty(_errorText))
      {
        this.Zone.ErrorText = string.Format("{0}<br />{1}", this.Zone.ErrorText,
                               _errorText);
      }      
      base.OnPreRender(e);
    }