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

妖精的绣舞 提交于 2019-12-23 01:24:18

问题


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;
  }
 }

回答1:


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);
}


来源:https://stackoverflow.com/questions/2921491/how-to-set-an-error-message-from-editorpart-when-applychanges-returns-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!