为什么我不能从事件接收器调用我的 Web 部件(用户控件)中的 public 方法?

Why can I not call a public method in my Web Part (User Control) from an Event Receiver?

我添加了这个事件接收器(基于我发现的 here):

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace PostTravelWizard.PostTravelItemEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class PostTravelItemEventReceiver : SPItemEventReceiver
    {
       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);
           //GeneratePDF(); <= "The name 'GeneratePDF' does not exist in the current context
       }

       /// <summary>
       /// The list received a context event.
       /// </summary>
       public override void ContextEvent(SPItemEventProperties properties)
       {
           base.ContextEvent(properties);
           // TODO: What "context event" occurs here? To what event should I respond?
       }

    }
}

我希望,当列表更新时(从客户端 side/Javascript),然后在代码隐藏中检索这些值以生成 PDF 文件。我的 *.ascx.cs 文件中有这段代码:

public partial class PostTravelWizardWebPartUserControl : UserControl
{
    . . .

    public void GeneratePDF(PostTravelData ptd)
    {
        ;//bla
    }

...但它不允许我从事件接收器调用 GeneratePDF() - 它失败并显示

The name 'GeneratePDF' does not exist in the current context

为什么?我该如何纠正这个 asunto?

GeneratePDFPostTravelWizardWebPartUserControlclass的实例方法。为了从 PostTravelItemEventReceiver 调用它,您必须创建 PostTravelWizardWebPartUserControl 的实例或让方法 (GeneratePDF) 成为 static.

案例一

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);
    new PostTravelWizardWebPartUserControl().GeneratePDF();
}

案例二

public partial class PostTravelWizardWebPartUserControl : UserControl
{
    public static void GeneratePDF(PostTravelData ptd)
    {
        ;//bla
    }
}


public class PostTravelItemEventReceiver : SPItemEventReceiver
{
    base.ItemAdded(properties);
    PostTravelWizardWebPartUserControl.GeneratePDF();
}