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

标签 c# web-parts

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

关于c# - 如何在 ApplyChanges 返回 false 时从 EditorPart 设置错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2921491/

相关文章:

c# - JSON 序列化速度慢

c# - 从 C# 代码生成包含触发器的 WPF 样式

c# - 使用图形与 WPF 应用程序连接到 Azure AD api

c# - 按值从列表中删除项目

asp.net-mvc - 在 SharePoint 2013 可视化 Web 部件中使用 Razor 引擎

SharePoint WebPart 显示两次 - 帮助!

SharePoint 2007 中的 Java/JSP 编码

c# - 为什么 `Microsoft.JScript` 在代码隐藏中工作,但在 `<% %>` 中不起作用?

c# - SharePoint 2013 更新面板空闲

c# - 将 html 添加到 CreateChildControls() 方法