Skip to main content
 首页 » 编程设计

c#之如何在 ApplyChanges 返回 false 时从 EditorPart 设置错误消息

2024年02月24日44qq78292959

我正在使用 WebPartManager 开发一个自定义的 ASP.Net WebPart,我也在创建一个自定义的 EditorPart。对于它的 EditorPart.ApplyChanges 方法,我在出现错误时将返回值设置为 false。

在 EditorZone 中,我收到一条标准错误消息,表明编辑器发生了一些错误,但我想更改该消息。 那可能吗?像...

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

请您参考如下方法:

我在 social msdn 中找到了一个解决方案,但我不确定它是否正确,因为它没有很好的记录。您必须在 PreRender 方法中设置错误,如下所示:

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