【问题标题】:How to set an error message from EditorPart when ApplyChanges returns false?当 ApplyChanges 返回 false 时,如何从 EditorPart 设置错误消息?
【发布时间】:2011-02-24 16:13:32
【问题描述】:

我正在使用 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;
  }
 }

【问题讨论】:

    标签: c# web-parts


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2015-08-02
      • 2012-03-20
      • 2019-04-24
      • 1970-01-01
      相关资源
      最近更新 更多