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

本文关键字:控件 方法 用户 不能 事件 接收器 部件 Web 调用 为什么 | 更新日期: 2023-09-27 18:13:44

我已经添加了这个事件接收器(基于我在这里发现的):

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

我希望,当一个列表被更新(从客户端/Javascript),然后在代码后检索这些值,以生成PDF文件。我在我的*. asx .cs文件中有以下代码:

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

…但它不让我从事件接收器调用GeneratePDF() -它失败了

名称'GeneratePDF'在当前上下文中不存在

为什么?我怎样才能纠正这个错误呢?

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

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

案例1

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

案例2

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

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